Skip to content

Cannot create an object with a trailing slash, eg foo/ #1909

@hughperkins

Description

@hughperkins

Describe the bug

When I try to create file with a trailing slash, eg using the following code, the object is created in s3 without a trailing slash. Using aws s3api, or using the AWS web console, I can create an object with a trailing slash correctly (i.e. represents an empty folder. I have a use-case for this).

    func createFolder(in bucketName: String, at path: String) async throws {
        let normalizedBucketName = normalizeBucketName(bucketName)
        let normalizedPath = path.hasSuffix("/") ? path : path + "/"
        print("normalizedPath", normalizedPath)
        
        let input = PutObjectInput(
            body: .data(Data()),
            bucket: normalizedBucketName,
            key: normalizedPath
        )
        do {
            _ = try await s3Client.putObject(input: input)
        } catch {
            print("Error creating folder: \(error)")
            throw error
        }
}

Expected Behavior

Expect to create an empty file with a trailing slash, which is interpreted by some UIs, such as aws web console, as an emptyt folder.

Current Behavior

The traling slash is truncated from the object's key, so eg the object foo/ is created as foo.

Reproduction Steps

Run the following code, with a bucket you have write access to, and a pth of 'foo/'. In AWS Web Console, you can see that the file is created with name foo instead of foo/

func createFolder(in bucketName: String, at path: String) async throws {
        let normalizedBucketName = normalizeBucketName(bucketName)
        let normalizedPath = path.hasSuffix("/") ? path : path + "/"
        print("normalizedPath", normalizedPath)
        
        let input = PutObjectInput(
            body: .data(Data()),
            bucket: normalizedBucketName,
            key: normalizedPath
        )
        do {
            _ = try await s3Client.putObject(input: input)
        } catch {
            print("Error creating folder: \(error)")
            throw error
        }
}

Possible Solution

I hypothesize that the library is removing trailing slashes from object paths.

Additional Information/Context

No response

AWS SWIFT SDK version used

1.2.43

Compiler and Version used

Xcode 16.2, swift 5

Operating System and version

MacOS 15.3.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions