AmazonS3 putObject与InputStream的长度例如 [英] AmazonS3 putObject with InputStream length example

查看:2607
本文介绍了AmazonS3 putObject与InputStream的长度例如的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文件上载到S3用java - 这是我到目前为止已有:

  AmazonS3 S3 =新AmazonS3Client(新BasicAWSCredentials(XX,YY));

名单<斗>水桶= s3.listBuckets();

s3.putObject(新PutObjectRequest(buckets.get(0).getName(),文件名,流,新ObjectMetadata()));
 

该文件被上传,但是当我不设置内容长度则会出现警告:

  com.amazonaws.services.s3.AmazonS3Client putObject:为流&gt没有指定内容长度;数据。流内容将被缓存在内存中,并可能导致内存不足的错误。
 

这是一个文件,我上传和变量是的InputStream ,从中我可以得到这样的字节数组: IOUtils.toByteArray(流)

所以,当我尝试设置内容长度和MD5(此处取自 )是这样的:

  //获取MD5哈希值的base64
消息摘要消息摘要= MessageDigest.getInstance(MD5);
messageDigest.reset();
messageDigest.update(IOUtils.toByteArray(流));
byte []的resultByte = messageDigest.digest();
字符串hashtext =新的String(Hex.en $ C $环己烷(resultByte));

ObjectMetadata元=新新Ob​​jectMetadata();
meta.setContentLength(IOUtils.toByteArray(流).length);
meta.setContentMD5(hashtext);
 

这将导致以下错误回来从S3:

  

您指定的内容,MD5是无效的。

我是什么做错了吗?

任何帮助AP preciated!

PS 我在谷歌应用程序引擎 - 我无法将文件写入到磁盘或<一href="http://www.java2s.com/$c$c/JavaAPI/java.io/FilecreateTempFileString$p$pfixStringsuffix.htm">create一个临时文件因为AppEngine上不支持的FileOutputStream。

解决方案

监守原来的问题一直没有回答,我只好碰到同样的问题,对于MD5问题的解决方案是,S3不希望的十六进制EN codeD字符串的MD5我们通常思考的问题。

相反,我不得不这样做。

  //内容是传递的InputStream
byte []的resultByte = DigestUtils.md5(内容);
字符串streamMD5 =新的String(Base64.en codeBase64(resultByte));
metaData.setContentMD5(streamMD5);
 

从本质上讲,他们想要的MD5值什么的Base64 EN codeD原始MD5的字节数组,而不是十六进制字符串。当我切换到这一点,开始为我伟大的工作。

I am uploading a file to S3 using java -- this is what I got so far:

AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("XX","YY"));

List<Bucket> buckets = s3.listBuckets();

s3.putObject(new PutObjectRequest(buckets.get(0).getName(), fileName, stream, new ObjectMetadata()));

The file is being uploaded but a WARNING is raised when I am not setting the content length:

com.amazonaws.services.s3.AmazonS3Client putObject: No content length specified for stream > data.  Stream contents will be buffered in memory and could result in out of memory errors.

This is a file I am uploading and the stream variable is an InputStream, from which I can get the byte array like this: IOUtils.toByteArray(stream).

So when I try to set the content length and MD5 (taken from here) like this:

// get MD5 base64 hash
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(IOUtils.toByteArray(stream));
byte[] resultByte = messageDigest.digest();
String hashtext = new String(Hex.encodeHex(resultByte));

ObjectMetadata meta = new new ObjectMetadata();
meta.setContentLength(IOUtils.toByteArray(stream).length);
meta.setContentMD5(hashtext);

It causes the following error to come back from S3:

The Content-MD5 you specified was invalid.

What am I doing wrong?

Any help appreciated!

P.S. I am on Google App Engine - I cannot write the file to disk or create a temp file because AppEngine does not support FileOutputStream.

解决方案

Becuase the original question was never answered, and I had to run into this same problem, the solution for the MD5 problem is that S3 doesn't want the Hex encoded MD5 string we normally think about.

Instead, I had to do this.

// content is a passed in InputStream
byte[] resultByte = DigestUtils.md5(content);
String streamMD5 = new String(Base64.encodeBase64(resultByte));
metaData.setContentMD5(streamMD5);

Essentially what they want for the MD5 value is the Base64 encoded raw MD5 byte-array, not the Hex string. When I switched to this it started working great for me.

这篇关于AmazonS3 putObject与InputStream的长度例如的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆