Amazon S3 GeneratePresignedUrlRequest - 新文件
Amazon S3 GeneratePresignedUrlRequest - new file
我想将一个新文件上传到 Amazon s3 到我的测试桶。
这里是 java 代码:
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(msec);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest("test-bucket", filename);
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
generatePresignedUrlRequest.setExpiration(expiration);
URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
然而我不断得到:
"The specified key does not exist." 用于文件名变量。
如何使此代码适用于新文件?
从表面上看,GeneratePresignedUrlRequest 是针对 S3 中的现有对象。
public GeneratePresignedUrlRequest(String bucketName, String key)
Creates a new request for generating a pre-signed URL that can be used as part of an HTTP GET request to access the Amazon S3 object stored under the specified key in the specified bucket.
Parameters:
bucketName - The name of the bucket containing the desired Amazon S3 object.
key - The key under which the desired Amazon S3 object is stored.
您可以在 AmazonS3Client class 中使用 putObject
方法之一。
PutObjectResult putObject(PutObjectRequest putObjectRequest)
Uploads a new object to the specified Amazon S3 bucket.
PutObjectResult putObject(String bucketName, String key, File file)
Uploads the specified file to Amazon S3 under the specified bucket and key name.
PutObjectResult putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata)
Uploads the specified input stream and object metadata to Amazon S3 under the specified bucket and key name.
将对象放入 S3 后,即可使用密钥实例化 GeneratePresignedUrlRequest 对象并获取 URL.
或者您可以使用 minio-java 客户端库,它是开源的并且与 AWS S3 兼容 API。
您可以查看 PresignedPutObject.java 示例。
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import org.xmlpull.v1.XmlPullParserException;
public class PresignedPutObject {
public static void main(String[] args)
throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException, MinioException {
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
// dummy values, please replace them with original values.
// Set s3 endpoint, region is calculated automatically
MinioClient s3Client = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");
String url = s3Client.presignedPutObject("my-bucketname", "my-objectname", 60 * 60 * 24);
System.out.println(url);
}
}
希望对您有所帮助。
免责声明:我为 Minio
工作
我想将一个新文件上传到 Amazon s3 到我的测试桶。
这里是 java 代码:
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(msec);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest("test-bucket", filename);
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
generatePresignedUrlRequest.setExpiration(expiration);
URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
然而我不断得到:
"The specified key does not exist." 用于文件名变量。
如何使此代码适用于新文件?
从表面上看,GeneratePresignedUrlRequest 是针对 S3 中的现有对象。
public GeneratePresignedUrlRequest(String bucketName, String key)
Creates a new request for generating a pre-signed URL that can be used as part of an HTTP GET request to access the Amazon S3 object stored under the specified key in the specified bucket.
Parameters:
bucketName - The name of the bucket containing the desired Amazon S3 object.
key - The key under which the desired Amazon S3 object is stored.
您可以在 AmazonS3Client class 中使用 putObject
方法之一。
PutObjectResult putObject(PutObjectRequest putObjectRequest)
Uploads a new object to the specified Amazon S3 bucket.PutObjectResult putObject(String bucketName, String key, File file)
Uploads the specified file to Amazon S3 under the specified bucket and key name.PutObjectResult putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata)
Uploads the specified input stream and object metadata to Amazon S3 under the specified bucket and key name.
将对象放入 S3 后,即可使用密钥实例化 GeneratePresignedUrlRequest 对象并获取 URL.
或者您可以使用 minio-java 客户端库,它是开源的并且与 AWS S3 兼容 API。
您可以查看 PresignedPutObject.java 示例。
import io.minio.MinioClient; import io.minio.errors.MinioException; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; import org.xmlpull.v1.XmlPullParserException; public class PresignedPutObject { public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException, MinioException { // Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are // dummy values, please replace them with original values. // Set s3 endpoint, region is calculated automatically MinioClient s3Client = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY"); String url = s3Client.presignedPutObject("my-bucketname", "my-objectname", 60 * 60 * 24); System.out.println(url); } }
希望对您有所帮助。
免责声明:我为 Minio
工作