简体   繁体   English

使用 java sdk 将带有用户元数据的大文件上传到 Amazon S3

[英]Uploading large files with user metadata to Amazon S3 using java sdk

I'm trying to use the Java API to upload large files, but I need to also add user metadata.我正在尝试使用 Java API 上传大文件,但我还需要添加用户元数据。 If I just use如果我只是使用

TransferManager tm = new TransferManager(new BasicAWSCredentials(accessKey, secretKey));

Upload upload = tm.upload(AmazonS3Manager.bucketName, imageKey, file);
upload.waitForCompletion();

Then everything works fine, but if I use:然后一切正常,但如果我使用:

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.length());
metadata.addUserMetadata("My key", "My value");
FileInputStream input = new FileInputStream(file);
Upload upload = tm.upload(AmazonS3Manager.bucketName, imageKey, input, metadata);

Then it doesn't work, and I get the following output in the console:然后它不起作用,我在控制台中得到以下 output :

Jul 5, 2011 4:33:15 PM com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Sending Request: POST https://mybucket.s3.amazonaws.com /test.jpg Parameters: (uploads: null, ) Headers: (Content-Type: application/x-www-form-urlencoded; charset=utf-8, x-amz-meta-length: 312612077, )
Jul 5, 2011 4:33:16 PM com.amazonaws.http.AmazonHttpClient handleResponse
INFO: Received successful response: 200, AWS Request ID: 2A5B3538795CE730
Jul 5, 2011 4:33:16 PM com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Sending Request: PUT https://mybucket.s3.amazonaws.com /test.jpg Parameters: (uploadId: rwUlbXqtRyMUWiVYKzGqRQH90fVLi9_|Secret Key Removed|_w--, partNumber: 1, ) Headers: (Content-Length: 5242880, Content-Type: application/x-www-form-urlencoded; charset=utf-8, )
Jul 5, 2011 4:34:00 PM com.amazonaws.http.AmazonHttpClient handleResponse
INFO: Received successful response: 200, AWS Request ID: 5E5AF291FBDBDD36
Jul 5, 2011 4:34:00 PM com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Sending Request: PUT https://mybucket.s3.amazonaws.com /test.jpg Parameters: (uploadId: rwUlbXqtRyMUWiVYKzGqRQH90fVLi9_|Secret Key Removed|_w--, partNumber: 2, ) Headers: (Content-Length: 5242880, Content-Type: application/x-www-form-urlencoded; charset=utf-8, )
Jul 5, 2011 4:34:00 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (java.io.IOException) caught when processing request: Read error
Jul 5, 2011 4:34:00 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request
Jul 5, 2011 4:34:00 PM com.amazonaws.http.AmazonHttpClient executeHelper
WARNING: Unable to execute HTTP request: null
Jul 5, 2011 4:34:00 PM com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Sending Request: DELETE https://mybucket.s3.amazonaws.com /test.jpg Parameters: (uploadId: rwUlbXqtRyMUWiVYKzGqRQH90fVLi9_|Secret Key Removed|_w--, ) Headers: (Content-Type: application/x-www-form-urlencoded; charset=utf-8, )
Jul 5, 2011 4:34:01 PM com.amazonaws.http.AmazonHttpClient handleResponse
INFO: Received successful response: 204, AWS Request ID: 0EFC3F8D0FA6097E

Any help greatly appreciated!非常感谢任何帮助!

You can use following code for your file upload:您可以使用以下代码进行文件上传:

    ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(file.length());
        metadata.addUserMetadata("My key", "My value");
        FileInputStream input = new FileInputStream(file);
        PutObjectRequest putObjectRequest = new PutObjectRequest(AmazonS3Manager.bucketName, imageKey, input, metadata);
s3client.putObject(putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead));

Thanks谢谢

You may also need to set content type and, possibly, checksum as it can't be determined from input stream.您可能还需要设置内容类型,并且可能还需要设置校验和,因为它无法从输入 stream 中确定。 This has some information for similar situation http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#putObject(com.amazonaws.services.s3.model.PutObjectRequest )这有一些类似情况的信息http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#putObject(com.amazonaws.services.s3.modelP )。

If you are trying to upload more than 5 GB file with updating metadata then amazon not allowing to do so.如果您尝试上传超过 5 GB 的文件并更新元数据,那么亚马逊不允许这样做。 Because amazon s3 not providing copy operation for more then 5Gb file.因为 amazon s3 不提供超过 5Gb 文件的复制操作。

Amazon S3 recently started to support copy operation wtih multipart operation so for that i can not say you something yet. Amazon S3 最近开始支持多部分操作的复制操作,因此我还不能说什么。

https://forums.aws.amazon.com/thread.jspa?messageID=256605 https://forums.aws.amazon.com/thread.jspa?messageID=256605

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用适用于Amazon S3存储桶的Java SDK下载大量文件 - Download a Large Number of Files Using the Java SDK for Amazon S3 Bucket 使用 Java 在 Amazon S3 上上传大尺寸(超过 1 GB)文件:大文件临时占用服务器中的大量空间 - Uploading large size (more than 1 GB) file on Amazon S3 using Java: Large files temporary consuming lot of space in server AWS Amazon S3 Java SDK-在上传大文件时过期时刷新凭证/令牌 - AWS Amazon S3 Java SDK - Refresh credentials / token when expired while uploading large file 使用Java SDK将多个文件批处理到Amazon S3 - Batching multiple files to Amazon S3 using the Java SDK 从Web浏览器上传大文件并传输到Amazon S3 - Uploading large files from web browser and transferring to Amazon S3 将大文件上传到 Amazon S3 时的问题 - Problems when uploading large files to Amazon S3 使用java SDK在S3 AWS上传文件时无法设置元数据 - Unable to set metadata while uploading file on S3 AWS using java SDK 使用AWS开发工具包Java上传S3 - Uploading S3 using AWS SDK Java 将文件上传到 Amazon S3 存储桶。 使用适用于 Java v2 的 AWS 开发工具包 - Issuing uploading file to Amazon S3 Bucket. using the AWS SDK for Java v2 使用 Microsoft Graph SDK for java 将带有元数据的大文件上传到 SharePoint - Uploading large file to SharePoint with metadata using Microsoft Graph SDK for java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM