[英]Error code 400 returns when trying to upload file to Amazon S3
I am a newbie to AWS and right now I am trying to write a standalone Java application to upload a PDF to AWS S3.我是 AWS 的新手,现在我正在尝试编写一个独立的 Java 应用程序来将 PDF 上传到 AWS S3。 However, error: 400 is returned.但是,返回错误:400。
Can anybody give me some general directions on how to troubleshoot this error?任何人都可以给我一些有关如何解决此错误的一般说明吗?
public class App {
private static String PDF_PATH = "/tmp/pdf-test.pdf";
public static void main(String[] args) throws IOException {
// prepare AWS credential
BasicAWSCredentials awsCreds = new BasicAWSCredentials("xxx",
"yyy");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("aaa")
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
// upload a test PDF
byte[] pdfDoc = Files.readAllBytes((new File(PDF_PATH)).toPath());
PutObjectRequest request = new PutObjectRequest("aaa", "bbb",
new String(Base64.getEncoder().encode(pdfDoc)));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/pdf");
metadata.setContentLength(pdfDoc.length);
request.setMetadata(metadata);
s3Client.putObject(request);
}
} }
To expand on what @AlexGoja said, there are three constructors for a PutObjectRequest
.为了扩展@AlexGoja 所说的内容, PutObjectRequest 有三个构造PutObjectRequest
。 One of them takes three Strings. 其中之一需要三个字符串。 However, the third string parameter is not a Base64 encoded file.但是,第三个字符串参数不是 Base64 编码的文件。 I'm not sure where you got that.我不确定你从哪里得到的。 You want to use the constructor that takes a File to upload the file:您想使用带有 File 的构造函数来上传文件:
public class App {
private static String PDF_PATH = "/tmp/pdf-test.pdf";
public static void main(String[] args) throws IOException {
// prepare AWS credential
BasicAWSCredentials awsCreds = new BasicAWSCredentials("xxx",
"yyy");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("aaa")
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
// upload a test PDF
File pdfFile = new File(PDF_PATH);
PutObjectRequest request = new PutObjectRequest("aaa", "bbb", pdfFile );
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/pdf");
metadata.setContentLength(pdfFile.length());
request.setMetadata(metadata);
s3Client.putObject(request);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.