所以我试图将文件上传到s3存储桶中以供亚马逊使用。但是,我需要一个HTTP网址。你们知道我这样做的方式吗?我现在正在使用nodejs,我只能获得签名的URL。任何帮助将不胜感激。
答案 0 :(得分:0)
为此必须使用GeneratePresignedUrlRequest
课程。
public static String writeToS3(File file, String folder) {
com.amazonaws.services.s3.AmazonS3 s3Client = new com.amazonaws.services.s3.AmazonS3Client(
getAWSCredentials());
String key = file.getName();
System.out.println("Going to write to key " + folder + key + " in S3");
s3Client.putObject(BUCKET, folder + key, file);
// This generates a URL that can be used to download the uploaded file.
Date expiration = new Date();
long msec = expiration.getTime();
msec += S3_URL_VALIDITY_PERIOD_MILLISEC;
expiration.setTime(msec);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
BUCKET, folder + key);
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
generatePresignedUrlRequest.setExpiration(expiration);
URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
if (s == null) {
System.out.println("Url returned was null");
return null;
}
System.out.println("S3 Url: " + s.toString());
return s.toString();
}
可在此处找到完整的Java代码:http://design-interviews.blogspot.in/2015/07/java-code-to-read-and-write-objects-in-AWS-S3.html