Generate S3 policy documents in Swift
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Sources/S3Policy
Tests
.gitignore
Package.swift
README.md

README.md

S3Policy

Generate your S3 upload policies in Swift.

For signing the policy you can use https://github.com/darkFunction/S3SignerAWS

Example use

Here we create the encoded policy and sign it using S3SignerAWS.

    import S3SignerAWS
    import S3Policy

    ...

    let timestamp = Date().iso8601(compact: true)
    let signer = S3Signer(accessKey: <awsAccessKey>, secretKey: <awsSecretKey>, region: <region>)
    let credential = "\(<awsAccessKey>)/\(signer.credentialScope(timeStampShort: timestamp.short))"
        
    let policy = S3Policy(
        expiration: <expiration>,
        conditions: [
            .exact(.bucket, <bucket>),
            .startsWith(.key, <path>),
            .exact(.acl, <acl>),
            .startsWith(.contentType, "image/"),
            .contentLength(0...<maxBytes>),
            .exact(.xAmzAlgorithm, SigningAlgorithm.hmacSha256.rawValue),
            .exact(.xAmzCredential, credential),
            .exact(.xAmzDate, timestamp.full)
        ]
    )

    let policyBase64 = try policy.json().base64()
    let policySignature = try signer.createSignature(stringToSign: policyBase64, timeStampShort: timestamp.short)