Skip to content

Commit

Permalink
s3: Add basic ACL to uploading files
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonycastelli committed May 2, 2018
1 parent 94b55a0 commit 8eb91b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Sources/S3Storage/ACL+Header.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ACL+Header.swift
// S3Storage
//
// Created by Anthony Castelli on 5/2/18.
//

import Foundation
import HTTP

protocol Header {
var header: [String : String]? { get }
}

extension PredefinedACL: Header {
var header: [String : String]? {
switch self {
case .default: return nil

// For Object and Bucket
case .private: return ["x-amz-acl": "private"]
case .publicRead: return ["x-amz-acl": "public-read"]
case .publicReadWrite: return ["x-amz-acl": "public-read-write"]
case .awsExecRead: return ["x-amz-acl": "aws-exec-read"]
case .authenticatedRead: return ["x-amz-acl": "authenticated-read"]

// For Object
case .bucketOwnerRead: return ["x-amz-acl": "bucket-owner-read"]
case .bucketOwnerFullControl: return ["x-amz-acl": "bucket-owner-full-control"]

// Bucket
case .logDeliveryWrite: return nil
}
}
}


13 changes: 12 additions & 1 deletion Sources/S3Storage/S3Adapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,18 @@ extension S3Adapter {
guard let url = URL(string: self.region.host + bucket.finished(with: "/") + object) else {
throw S3AdapterError(identifier: "write", reason: "Couldnt not generate a valid URL path.", source: .capture())
}
let headers = try self.generateAuthHeader(.PUT, urlString: url.absoluteString, payload: .bytes(content))

// Create any ACL headers we need
var aclHeaders = [String : String]()
if let metadata = metadata {
if let predefinedACL = metadata.predefinedACL?.header {
if let key = predefinedACL.keys.first, let value = predefinedACL.values.first {
aclHeaders[key] = value
}
}
}

let headers = try self.generateAuthHeader(.PUT, urlString: url.absoluteString, headers: aclHeaders, payload: .bytes(content))

let request = Request(using: container)
request.http.method = .PUT
Expand Down

0 comments on commit 8eb91b2

Please sign in to comment.