简体   繁体   English

上传到Amazon S3时命名文件

[英]Naming files when uploading to Amazon S3

I am trying to upload files to Amazon S3, nothing special. 我正在尝试将文件上传到Amazon S3,没什么特别的。 I have managed to do the actual upload, and the file uploads successfully. 我已设法进行实际上传,文件上传成功。 The only issue that's left is that I cannot change the name of the file in S3. 剩下的唯一问题是我无法在S3中更改文件的名称。 It seems that by default, the name of the file is being set the same as the secret key. 似乎默认情况下,文件的名称设置与密钥相同。 It could be that I am sending the secret key as a parameter where I should send the name of the file instead. 可能是我发送密钥作为参数,我应该发送文件的名称。 However, I tried changing the parameters around and errors prop up. 但是,我尝试更改参数和错误支持。

Below please find the code I am using: 请在下面找到我正在使用的代码:

Bucket bucket = client.createBucket("testBucket", Region.EU_Ireland);

    List<PartETag> partTags = new ArrayList<>();

    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(
            bucket.getName(), secretAmazonKey);

    InitiateMultipartUploadResult result = client
            .initiateMultipartUpload(request);
    File file = new File(filePath);
    long contentLength = file.length();
    long partSize = 8 * 1024 * 1024;

    try {
        // Uploading the file, part by part.
        long filePosition = 0;

        for (int i = 1; filePosition < contentLength; i++) {
            // Last part can be less than 8 MB therefore the partSize needs
            // to be adjusted accordingly
            partSize = Math.min(partSize, (contentLength - filePosition));

            // Creating the request for a part upload
            UploadPartRequest uploadRequest = new UploadPartRequest()
                    .withBucketName(bucket.getName()).withKey(secretAmazonKey)
                    .withUploadId(result.getUploadId()).withPartNumber(i)
                    .withFileOffset(filePosition).withFile(file)
                    .withPartSize(partSize);

            // Upload part and add response to the result list.
            partTags.add(client.uploadPart(uploadRequest).getPartETag());

            filePosition += partSize;
        }
    }

    catch (Exception e) {
        client.abortMultipartUpload(new AbortMultipartUploadRequest(bucket
                .getName(), secretAmazonKey, result.getUploadId()));
    }

    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(
            bucket.getName(), secretAmazonKey, result.getUploadId(), partTags);

    client.completeMultipartUpload(compRequest);

Any help is much appreciated. 任何帮助深表感谢。

Thanks a lot :) 非常感谢 :)

The key in your upload requests is actually your object (file) key (name), and not your AWS secret key. 上传请求中的key实际上是您的对象(文件)密钥(名称),而不是您的AWS密钥。 Whenever you instantiate your client instance, this is the time where you specify your AWS credentials. 每当您实例化client实例时,都是指定AWS凭据的时间。

Could you be more specific regarding the errors you are seeing when doing this? 您是否可以更具体地了解您在执行此操作时遇到的错误?

Well, I used Amazon S3 for the first time recently and was able to upload a file as below: 好吧,我最近第一次使用了Amazon S3,并且能够上传如下文件:

public void saveMinutes(Minutes minutes, byte [] data)
{

  AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(amazonS3AccessKey, amazonS3SecretAccessKey));
  ObjectMetadata metaData = new ObjectMetadata();
  metaData.setContentLength(data.length);
  metaData.setContentType("application/pdf");
  s3.putObject(new PutObjectRequest(amazonS3MinutesBucketName, minutes.getFileName(), new ByteArrayInputStream(data), metaData));
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM