[英]Amazon S3: `key` is the object being uploaded to bucket instead of the file
请检查以下代码
import java.io.File;
import java.io.IOException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class UploadObjectSingleOperation {
private static String bucketName = "*******";
private static String keyName = "************";
private static String uploadFileName = "C:/Users/Yohan/Desktop/asdasd.html";
public static void main(String[] args) throws IOException {
BasicAWSCredentials creds = new BasicAWSCredentials(keyName, "**********");
AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).withRegion(Regions.AP_SOUTH_1).build();
// AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
try {
System.out.println("Uploading a new object to S3 from a file\n");
File file = new File(uploadFileName);
s3client.putObject(new PutObjectRequest(
bucketName, keyName, file));
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which " +
"means your request made it " +
"to Amazon S3, but was rejected with an error response" +
" for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which " +
"means the client encountered " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
}
确定,因此上面的代码已用于将文件上传到Amazon S3 Bucket。 我的S3铲斗距离我的客户Asia Pacific - Mumbai
最近。
上面的代码工作正常,但是我注意到了以下内容。
key
。 实际文件未上传。 请检查下图。 为什么会这样呢? 当我使用S3的Web界面上传文件时,它可以正常工作。
发现错误。 我的问题中的代码来自Amazon教程-http: //docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html
但是,我确定它是不正确或已弃用的。
注意以下行
s3client.putObject(new PutObjectRequest(bucketName, keyName, file));
为此,您必须传递带有扩展名的文件名而不是keyName
。 例如, websitepage.html
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.