java - 如何将文件上传到 AWS 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 存储桶凭证、权限和依赖项。
我应该在代码中进行哪些更改才能将我的文件上传到所需的存储桶?
解决方案
看起来好像您要么拥有错误版本的 Jackson 库,要么以某种方式链接到它们的多个版本。
AWS for Java SDK 发行版包含一个第三方/lib 目录,其中包含构建该版本的 SDK 应使用的所有(正确版本)库。根据您使用的 SDK 的哪些功能,您可能不需要所有这些功能,但这些是您应该使用的特定 3rd 方库。
推荐阅读
- linux - 无法在 ubuntu 中使用 ansible 将文件复制到多个服务器
- navigation - 使用超声波传感器检测楼梯 - 仅使用传感器
- python - 如何在熊猫中用字典映射列值的一部分?
- python - ModbusTCP 流量在 WireShark 中显示为 TCP 数据包
- sql - 如何将日期更改为 DD/MM/YYY 格式,然后使用 postgresql 更改为文本?
- algorithm - 访问节点具有可选输出的图的算法
- c - C - !isalpha 将 unsigned char 类型的字符视为非字母字符
- python - Python max() 与 min()
- r - lme4 1.1.26 版不再打印固定效果的 p 值?
- matlab - 计算的准确性