Java 在AWS s3中上载文件时,元数据道具显示访问被拒绝
我正在尝试将一个文件从Java代码上传到S3和CloudFrontJava 在AWS s3中上载文件时,元数据道具显示访问被拒绝,java,amazon-web-services,amazon-s3,Java,Amazon Web Services,Amazon 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.comm
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,那么我的对象的元数据属性将显示正确的值
但是如果我在将文件上传到S3后尝试调用uploadToCloudFront()
,如代码所示。然后我的元数据道具显示访问被拒绝
是否有人在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;
}