Skip to content

Amazon AWS S3

Andrew Wagner edited this page Aug 8, 2019 · 2 revisions

We include declarations for some of the most common AWS S3 requests, including the ability to generate pre-signed URLS.

If there is an endpoint you need that isn't already defined, create an issue and I will do my best to add it, or even better, submit a pull-request.

Configuration

You will have to configure a few properties on the service before you can use it.

  • Region: bucket region e.g. "us-west-1"
  • Bucket: the bucket name
  • Access Key: the access key you generated for this S3 bucket
  • Secret Key: the secret key you generated for this S3 bucket

The easiest way to do this is to update the shared instance.

AWS.S3.shared = AWS.S3(
    region: "region",
    bucket: "bucket.name",
    accessKey: "ACCESS",
    secretKey: "SECRET"
)

Endpoints

Get Object

Retrieve an object.

AWS.S3.GetObject(name: "some-object.txt").makeRequest() { result in }

Pre-sign the url for getting an object

let url = try AWS.S3.GetObject(name: "some-object.txt").presignURL()

Add Object

Upload an new object.

let body = "Example Body".data(using: .utf8)!
AWS.S3.AddObject(name: "some-object.txt").makeRequest(with: body) { result in }

Pre-sign the url for adding an object

let url = try AWS.S3.AddObject(name: "some-object.txt").presignURL()

Delete Object

Delete an existing object.

AWS.S3.DeleteObject(name: "some-object.txt").makeRequest() { result in }

Pre-sign the url for deleting an object

let url = try AWS.S3.DeleteObject(name: "some-object.txt").presignURL()

Mocking

There is an extra expectation available for presigned URLs.

let mock = AWS.S3.shared.startMocking()
mock.expectPresigning(AWS.S3.GetObject(name: "test.txt"), returning: URL(string: "http://example.com")!)

Clone this wiki locally