withCannedAcl(CannedAccessControlList.PublicRead) not working for Contabo Object storage

Viewed 41

Below is my source code:

public void uploadFile(String filedir, String name, byte[] content) {
    File file = new File(name);
    try (FileOutputStream iofs = new FileOutputStream(file)) {
        iofs.write(content);
        PutObjectRequest putObj =
                new PutObjectRequest(defaultBucketName, defaultBaseFolder + "/" + filedir + "/" + file.getName(), file)
                        .withCannedAcl(CannedAccessControlList.PublicRead);
        PutObjectResult putObjectResult =
                amazonS3Client.putObject(putObj);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Expected Behavior: After executing the above code, the file stored at Contabo object storage should be "Public".

Actual Behavior: But it is not Public after upload. It remains as private as below: Click here to see object storage state

aws-java-sdk version: 1.12.215

1 Answers

I have also encountered this issue. I spent few hours trying to figure out why the "Public Access" button is still inactive while exactly following Contabo's official documentation

As it turned out, files were available for download after adding them with correct ACL's even though the Public Access button is not green. I used the code below

public String store(MultipartFile file, String filePath) {
  try {
    PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, filePath, file.getInputStream(), null)
      .withCannedAcl(CannedAccessControlList.PublicRead);

    s3client.putObject(request);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return filePath;
}

As far as I know, in order to access uploaded resource you need to build URL by yourself. I followed the sheme visible in the URL generated in Contabo UI using "Make Public" slider.

Related