java - 在 AWS s3 中上传文件时,元数据道具显示拒绝访问
问题描述
我正在尝试将文件从我的 Java 代码上传到 S3 和 CloudFront。
public String uploadFile(String fileData, String fileName, String contentType, String extension){
try {
loggerUtils.log ("File Data" , fileData);
byte[] bI = org.apache.commons.codec.binary.Base64.decodeBase64 ((fileData.substring (fileData.indexOf (",") + 1)).getBytes ());
InputStream fis = new ByteArrayInputStream (bI);
AmazonS3 s3 = new AmazonS3Client ();
Region usWest02 = Region.getRegion (REGION);
s3.setRegion (usWest02);
ObjectMetadata metadata = new ObjectMetadata ();
metadata.setContentLength (bI.length);
metadata.setContentType (contentType + "/" + extension.substring (1));
metadata.setCacheControl ("public, max-age=0");
s3.putObject (BUCKET_NAME, fileName, fis, metadata);
s3.setObjectAcl (BUCKET_NAME, fileName, CannedAccessControlList.PublicRead);
URL s3Url = s3.getUrl(BUCKET_NAME, fileName);
s3Url.toExternalForm();
// return s3Url.toExternalForm();
return uploadToCloudFront (fileName, bI).toExternalForm ();
}
catch (Exception exception){
exception.printStackTrace ();
loggerUtils.log (exception.toString ());
}
return null;
}
public URL uploadToCloudFront(String fileName, byte[] imageByteArray) {
URL url;
try {
String cloudFrontURL = "************.cloudfront.net";
url = new URL("http://"+cloudFrontURL+ "/"+ fileName);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset","UTF-8");
connection.setRequestProperty("Content-Length", imageByteArray.length+"");
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.write(imageByteArray);
wr.flush();
wr.close();
connection.disconnect();
connection.getResponseCode();
if(connection.getResponseCode () == 200) { return url; } else return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
如果我只是将文件上传到 S3 Bucket ,那么我的对象的元数据属性将显示正确的值。
uploadToCloudFront()
但是,如果我在将文件上传到 S3 后尝试调用,如代码所示。然后我的 MetaData 道具显示访问被拒绝。
有没有人在使用 S3 和 CloudFront 时遇到过类似的问题。
解决方案
推荐阅读
- sql - 如何在 Informix 上使用函数和索引来加速查询
- plotly - 如何从plotly.js中的饼图切片中删除值标签?
- php - 通过PHP将每条数据从sql表中分离出来
- python - Django 2 - 登录身份验证问题:用户名的 MultiValueDictKeyError
- mysql - MySQL 插入选择集
- eslint - AirBnB Linter 在同一行中多次柯里化,使其太长
- javascript - javascript中的从现在开始的反应时刻功能?
- sql - 如何通过root用户获取数据库版本
- java - 使用 Apache POI 创建的 Excel 将点作为小数分隔符,表示大于 1000 的值,逗号表示较小的值
- c# - 在 C# 中使用 Google PhotosLibrary