请检查以下代码
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
最近的位置。
上面的代码工作正常,但我注意到以下内容。
答案 0 :(得分:0)
发现错误。我的问题中的代码来自亚马逊教程 - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html
但我确定它不正确或已弃用。
注意以下行
s3client.putObject(new PutObjectRequest(bucketName, keyName, file));
要使其工作,而不是keyName
,您必须传递带扩展名的文件名。例如websitepage.html
。