Report this

What is the reason for this report?

Spaces bucket vs key

Posted on February 15, 2024

I’ve got a Swift application that uses the Soto client library for S3 access. I’ve got it working fine, but I see there are a few different ways to get an object to the same place:

An example of my code is this:

    func
    put(data inData: Data, destPath inPath: String, contentType inContentType: String)
        async
        throws
    {
        let key = (inPath as NSString).lastPathComponent
        let path = (inPath as NSString).deletingLastPathComponent
        let putObjectRequest = S3.PutObjectRequest(
            acl: .publicRead,
            body: .data(inData),
            bucket: "\(path)",
            contentType: inContentType,
            key: key
        )
        let putResult = try await self.s3?.putObject(putObjectRequest)
        Self.logger.info("Uploaded file to: \(inPath)")
    }

I must’ve based that off some sample code, because it rubs me the wrong way. I prefer one where bucket is specified as the actual bucket name, and key is the full path and filename. I’ve tried it that way, and it works.

It also works if the endpoint I specify in the S3 client configuration takes the form https://my-bucket-name.region.digitaloceanspaces.com, and I specify an empty string for the bucket.

What’s the “right” way to do this, and does Amazon S3 work the same way?

UPDATE: It seems this is a supported behavior of Amazon S3 as well (Virtual Hosting of Buckets). For other reasons, it’s helpful in my app to make the endpoint URL as unique as possible, so I’ll be doing that, and using an empty string for the bucket argument and the full path and filename for the key argument.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.