我是新來的amazons3我想上傳一個文件到s3桶。所以我正在爲java編寫代碼,但我不知道如何指定桶的路徑。任何人都可以幫助我。 請幫助找到獲取s3存儲桶正確路徑的解決方案。亞馬遜s3桶路徑問題
public class SampleS3 {
private static final String SUFFIX = "/";
private static String existingBucketName = "s3-buckt path";
private static String AccessKey = "accesskey";
private static String SecretAccessKey = "SecretAccessKey";
private static String keyName = "mypic.JPG";
private static String filePath = "/home/dll files/samplefile.txt";
private static String amazonFileUploadLocationOriginal = existingBucketName+ "/";
public static void main(String[] args) throws IOException {
executeUpload();
}
private static void executeUpload() {
AWSCredentials credentials = new BasicAWSCredentials(AccessKey,
SecretAccessKey);
AmazonS3 s3Client = new AmazonS3Client(credentials);
String folderName = "testfolder";
createFolder(existingBucketName, folderName, s3Client);
String fileName = folderName + SUFFIX + "testvideo.mp4";
s3Client.putObject(new PutObjectRequest(existingBucketName, fileName,
new File(filePath))
.withCannedAcl(CannedAccessControlList.PublicRead));
}
public static void createFolder(String bucketName, String folderName,
AmazonS3 client) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
folderName + SUFFIX, emptyContent, metadata);
client.putObject(putObjectRequest);
}
}
當我運行程序我得到的錯誤是:
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The specified bucket does not exist (Service: Amazon S3; Status Code: 404; Error Code: NoSuchBucket; Request ID: 2D45010A48E44C42), S3 Extended Request ID: c7zzSgYO8bNUh9HoOVVmAbjjtG7G6w5tQmuSIqTmgf3cGqjclTrJS3WIXvPpV7Mt
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1389)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:902)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:607)
at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:376)
at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:338)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:287)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3826)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1405)
at com.qagate.s3.amazons3.SampleS3.createFolder(SampleS3.java:65)
at com.qagate.s3.amazons3.SampleS3.executeUpload(SampleS3.java:45)
at com.qagate.s3.amazons3.SampleS3.main(SampleS3.java:33)
顯示您到目前爲止嘗試過的代碼。 –
我想知道如何使用s3桶路徑 –
您的桶名稱有一個空間,我不認爲這是蒼蠅:http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions。 html –