如何将文件上传到AWS S3 Bucket?

时间:2018-06-07 06:47:06

标签: java amazon-web-services amazon-s3

我正在创建一个简单的应用程序,我想将文件上传到AWS S3存储桶。这是我的代码:

import java.io.File;
import java.io.IOException;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.fasterxml.jackson.*;

public class UploadFileInBucket {

    public static void main(String[] args) throws IOException {
        String clientRegion = "<myRegion>";
        String bucketName = "<myBucketName>";
        String stringObjKeyName = "testobject";
        String fileObjKeyName = "testfileobject";
        String fileName = "D:\\Attachments\\LICENSE";

        try {

            BasicAWSCredentials awsCreds = new BasicAWSCredentials("<myAccessKey>", "<mySecretKey>");
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .build();

            // Upload a text string as a new object.
            s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");

            // Upload a file as a new object with ContentType and title specified.
            PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("plain/text");
            metadata.addUserMetadata("x-amz-meta-title", "someTitle");
            request.setMetadata(metadata);
            s3Client.putObject(request);
        }
        catch(AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        }
        catch(SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}

我无法上传文件并收到错误消息:

Exception in thread "main" java.lang.NoSuchFieldError: 

    ALLOW_FINAL_FIELDS_AS_MUTATORS
        at com.amazonaws.partitions.PartitionsLoader.<clinit>(PartitionsLoader.java:52)
        at com.amazonaws.regions.RegionMetadataFactory.create(RegionMetadataFactory.java:30)
        at com.amazonaws.regions.RegionUtils.initialize(RegionUtils.java:64)
        at com.amazonaws.regions.RegionUtils.getRegionMetadata(RegionUtils.java:52)
        at com.amazonaws.regions.RegionUtils.getRegion(RegionUtils.java:105)
        at com.amazonaws.client.builder.AwsClientBuilder.getRegionObject(AwsClientBuilder.java:249)
        at com.amazonaws.client.builder.AwsClientBuilder.withRegion(AwsClientBuilder.java:238)
        at UploadFileInBucket.main(UploadFileInBucket.java:28)

我已添加了必需的AWS存储桶凭据,权限和依赖关系来执行此代码。

我应该在代码中进行哪些更改才能将文件上传到所需的存储桶?

2 个答案:

答案 0 :(得分:2)

看起来你要么拥有错误版本的Jackson库,要么以某种方式链接到它们的多个版本。

AWS for Java SDK发行版包含第三方/ lib目录,其中包含应构建SDK版本的库的所有(正确版本)。根据您使用的SDK的哪些功能,您可能不需要所有这些功能,但这些是您应该使用的特定第三方库。

答案 1 :(得分:1)

您需要将Jackson添加到类路径中。它的课程遗失了。 我不知道您需要哪个版本,但您可以从他们的gitpage下载它们:https://github.com/FasterXML/jackson/