我是amazons3的新手,我想将文件上传到s3存储桶。所以我在java中编写代码,但我不知道如何指定存储桶的路径。任何人都可以帮助我。 请帮助找到获取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)
答案 0 :(得分:2)
S3不像Linux文件系统。
您的S3存储包含在完全平坦的空间中包含对象的存储桶。 "文件夹"的概念是人为的:一个带有键的对象" foo"与密钥" path / to / bar"一起存在于同一个扁平桶名称空间中。没有单独的创建文件夹的步骤;你只需给你的对象一个键,其名称包含分隔符,如' /'并且您可以通过一些涉及公共前缀的LIST API将存储桶视为具有文件夹。
但是, buckets 必须先创建一次,然后再将其存储在其中。除其他事项外,您必须指出哪个区域应该托管进入该存储桶的对象。
答案 1 :(得分:0)
您提供的存储桶名称不正确或您没有创建存储桶。
您需要查看有关Bucket Restriction的文档。从文档
- 存储桶名称长度必须至少为3且不超过63个字符。
- 存储桶名称必须是一系列一个或多个标签。相邻标签由单个句点(。)分隔。桶名称可以 包含小写字母,数字和连字符。每个标签必须 以小写字母或数字开头和结尾。
- 不得将存储桶名称格式化为IP地址(例如, 192.168.5.4)。
- 使用带有SSL的虚拟托管样式存储桶时,SSL通配符证书仅匹配不包含句点的存储桶。上班 围绕这个,使用HTTP或编写自己的证书验证逻辑。我们建议您不要在桶中使用句点(“。”) 名。