[英]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.