我想将公共文件上传到公用文件夹,将私有文件上传到在aws3存储桶中创建的私有文件夹。
我应该对代码进行哪些更改。
@Override
public void uploadFile(String keyName, String uploadFilePath) {
try {
File file = new File(uploadFilePath);
s3client.putObject(new PutObjectRequest(bucketName + "/public", keyName, file));
logger.info("===================== Upload File - Done! =====================");
System.out.println("url is*****" + s3client.getUrl(bucketName + "/public", keyName).toString());
url = s3client.getUrl(bucketName+ "/public", keyName).toString();
Connection cn = new Connection();
cn.db();
} catch (AmazonServiceException ase) {
logger.info("Caught an AmazonServiceException from PUT requests, rejected reasons:");
logger.info("Error Message: " + ase.getMessage());
logger.info("HTTP Status Code: " + ase.getStatusCode());
logger.info("AWS Error Code: " + ase.getErrorCode());
logger.info("Error Type: " + ase.getErrorType());
logger.info("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
logger.info("Caught an AmazonClientException: ");
logger.info("Error Message: " + ace.getMessage());
}
}
答案 0 :(得分:1)
默认情况下,Amazon S3存储桶中的所有对象都是私有的。
如果您随后希望将特定文件夹公开,则可以使用 Bucket Policy (桶策略),例如:
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"PublicFolder",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::examplebucket/public/*"]
}
]
}
答案 1 :(得分:0)
存储桶,键的值的构造不正确
bucketName + "/public", keyName
正确构造存储桶,键的值:
bucketName, "public/" + keyName
请注意,对象键不应该包含前导/
,因此,如果您的keyName
现在有一个前导,那么这是错误的,应该予以解决...对象键没有前导/
并提供一个会引起问题。