Question

How do I set file permissions for files in a space using AWS SDK with java?

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.


Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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();
    }```

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