将文件上传到 AWS S3,并授予 public 访问权限
Upload a file to AWS S3 with public access granted
我有一个定义为 public 访问权限的 S3 存储桶,但是,当我将文件上传到其中并访问 URL 时,出现访问被拒绝错误:
{
Region region = Region.EU_WEST_1;
S3Client s3 = S3Client.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.region(region)
.build();
String result = putS3Object(s3, "my-bucket", "pics/15/12345.jpg", "/opt/pics/15/12345.jpg");
System.out.println(result);
}
// snippet-start:[s3.java2.s3_object_upload.main]
public static String putS3Object(S3Client s3,
String bucketName,
String objectKey,
String objectPath) {
try {
PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build(),
RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
// Return a byte array
private static byte[] getObjectFile(String filePath) {
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
这是我在存储桶中看到的信息:
Objects are the fundamental entities that are stored in Amazon S3. In order for other people to gain access to objects, you will have to explicitly grant them permissions.
我也试过 API
的 V2
我看到您使用的是 S3 的 V1 版本 API。尝试使用 V2。我从未见过将对象上传到您帐户中拥有的 Amazon S3 存储桶的问题。
试试这个例子:
如果您以前从未使用过 V2,我建议您尝试这个快速入门:https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
您的问题可能与以下事实有关:虽然您配置了 public 访问您的存储桶,可能通过取消选中其 Block public access
配置选项中的所有复选框,您仍然需要 enable public access 在创建对象时通过配置相应的对象 ACL 在对象本身上。
您可以在put object 操作中指定该object 的ACL。请考虑在您的代码中进行以下修改:
// snippet-start:[s3.java2.s3_object_upload.main]
public static String putS3Object(S3Client s3,
String bucketName,
String objectKey,
String objectPath) {
try {
PutObjectResponse response = s3.putObject(
PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
// this is the important modification, set a
// pre-established (canned) ACL of public-read for the object
.acl(ObjectCannedACL.PUBLIC_READ)
.build(),
RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
请参阅Java API and object ACLs的相关文档。
我有一个定义为 public 访问权限的 S3 存储桶,但是,当我将文件上传到其中并访问 URL 时,出现访问被拒绝错误:
{
Region region = Region.EU_WEST_1;
S3Client s3 = S3Client.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.region(region)
.build();
String result = putS3Object(s3, "my-bucket", "pics/15/12345.jpg", "/opt/pics/15/12345.jpg");
System.out.println(result);
}
// snippet-start:[s3.java2.s3_object_upload.main]
public static String putS3Object(S3Client s3,
String bucketName,
String objectKey,
String objectPath) {
try {
PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build(),
RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
// Return a byte array
private static byte[] getObjectFile(String filePath) {
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
这是我在存储桶中看到的信息:
Objects are the fundamental entities that are stored in Amazon S3. In order for other people to gain access to objects, you will have to explicitly grant them permissions.
我也试过 API
的 V2我看到您使用的是 S3 的 V1 版本 API。尝试使用 V2。我从未见过将对象上传到您帐户中拥有的 Amazon S3 存储桶的问题。
试试这个例子:
如果您以前从未使用过 V2,我建议您尝试这个快速入门:https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
您的问题可能与以下事实有关:虽然您配置了 public 访问您的存储桶,可能通过取消选中其 Block public access
配置选项中的所有复选框,您仍然需要 enable public access 在创建对象时通过配置相应的对象 ACL 在对象本身上。
您可以在put object 操作中指定该object 的ACL。请考虑在您的代码中进行以下修改:
// snippet-start:[s3.java2.s3_object_upload.main]
public static String putS3Object(S3Client s3,
String bucketName,
String objectKey,
String objectPath) {
try {
PutObjectResponse response = s3.putObject(
PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
// this is the important modification, set a
// pre-established (canned) ACL of public-read for the object
.acl(ObjectCannedACL.PUBLIC_READ)
.build(),
RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
请参阅Java API and object ACLs的相关文档。