AmazonS3 putObject与InputStream的长度例如

编程入门 行业动态 更新时间:2024-10-15 10:20:23
本文介绍了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 我在谷歌应用程序引擎 - 我无法将文件写入到磁盘或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的长度例如

本文发布于:2023-11-03 18:59:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度   putObject   InputStream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!