By lukeshay
I am using a space to store images for my web app. Those images need to be public so I can display them on the web app. How do I make those images public using the AWS SDK with Java? I know how to do it manually but obviously that is not scalable.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
I have not done this with Java, but usually with Laravel for example, what I would do is to sue the putFile method, which allows you to pass the this as an argument:
Storage::disk('spaces')->putFile('uploads', request()->file, 'public');
In your case what I could suggest is setting the x-amz-acl header to public-read that way each individual file would be available for everyone. For more information you could take a look at the documentation here:
https://developers.digitalocean.com/documentation/spaces/#object
Or another option would be to set your listing permissions to public so that all files in that listing would be available by default:
https://www.digitalocean.com/docs/spaces/how-to/set-file-listing-permissions/
Hope that this helps! Regards, Bobby
The above answer does not really help. I figured it out on my own and here is the code snippet in case anybody else has this question.
AmazonS3 awsBuckets =
AmazonS3ClientBuilder.standard()
.withCredentials(credentialsProvider)
.withEndpointConfiguration(new EndpointConfiguration(bucketUrl, "nyc3"))
.build();
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, converted);
putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
awsBuckets.putObject(putObjectRequest);
This worked for me
public String uploadBase64ToStorage(String filePath,String location,String fileName) {
String key = Config.getProperty("spaces", "key");
String secret = Config.getProperty("spaces", "secret");
String bucket = Config.getProperty("spaces", "bucket");
String endpoint = Config.getProperty("spaces", "endpoint");
String region = Config.getProperty("spaces", "region");
AWSCredentialsProvider awscp = new AWSStaticCredentialsProvider(
new BasicAWSCredentials(key, secret)
);
AmazonS3 space = AmazonS3ClientBuilder
.standard()
.withCredentials(awscp)
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(endpoint, region)
)
.build();
File file = new File(filePath);
String filepath = location+"/"+fileName; // /somefolder/someanotherfolder/testfile.jpg";
ObjectMetadata om = new ObjectMetadata();
om.setContentLength(file.length());
om.setContentType("image/jpg");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket,filepath,file).withCannedAcl(CannedAccessControlList.PublicRead);
space.putObject(putObjectRequest);
return space.getUrl(bucket, filepath).toString();
}```
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.