user361676
user361676

Reputation: 816

AWS relative path in S3 key name

Every documentation for AWS S3 says that although S3 key names look like paths, they are not paths. However, can anyone explain why the following code does not work? It seems like it's doing some kind of relative path check.

import com.amazonaws.PredefinedClientConfigurations;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import java.io.File;

public class S3Test {

    public static void main(String[] args) {
        AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
                .withClientConfiguration(
                        PredefinedClientConfigurations.defaultConfig().withMaxErrorRetry(10).withRequestTimeout(30_000))
                .withCredentials(new ProfileCredentialsProvider("myprofile"))
                .withRegion("us-east-1")
                .build();

        File file = new File("test.json");

        System.out.println("Start upload1");
        // this works
        amazonS3.putObject("mybucket", "a/../test.json", file);

        System.out.println("Start upload2");
        //this fails with error code 400  Invalid URI
        amazonS3.putObject("mybucket", "a/../../test.json", file);
    }
}

Upvotes: 0

Views: 880

Answers (1)

Paolo
Paolo

Reputation: 26074

This:

a/../test.json

translates to the same path level as the folder a. So after your upload command, your bucket will look like this:

- a/
- test.json

this:

a/../../test.json

on the other hand translates to one level above the folder a, which doesn't exist in the bucket, hence the error.

Upvotes: 1

Related Questions