Reputation: 6884
I would like to upload a new file to Amazon s3 to my test-bucket.
Here is the java code:
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);
However i keep getting:
"The specified key does not exist." for the filename var.
How do I make this code works with a new file?
Upvotes: 0
Views: 5422
Reputation: 1964
Alternatively you can use minio-java client library, its Open Source and compatible with AWS S3 API.
You can check PresignedPutObject.java example for the same.
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); } }
Hope it helps.
Disclaimer: I work for Minio
Upvotes: 0
Reputation: 11992
From the looks of it GeneratePresignedUrlRequest is for existing objects in 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.
You can use one of the putObject
methods in AmazonS3Client class.
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.
Once you put the object into S3, you can then use the key to instantiate a GeneratePresignedUrlRequest object and get the URL.
Upvotes: 1