-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from zdnk/s3-copy-file
Copy file API
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// S3+Copy.swift | ||
// S3 | ||
// | ||
// Created by Topic, Zdenek on 17/10/2018. | ||
// | ||
|
||
import Foundation | ||
import Vapor | ||
|
||
extension S3 { | ||
|
||
/// Copy file on S3 | ||
public func copy(file: LocationConvertible, to: LocationConvertible, headers: [String: String], on container: Container) throws -> EventLoopFuture<File.CopyResponse> { | ||
let builder = urlBuilder(for: container) | ||
let originPath = "\(file.bucket ?? defaultBucket)/\(file.path)" | ||
let destinationUrl = try builder.url(file: to) | ||
|
||
var awsHeaders: [String: String] = headers | ||
awsHeaders["x-amz-copy-source"] = originPath | ||
let headers = try signer.headers( | ||
for: .PUT, | ||
urlString: destinationUrl.absoluteString, | ||
headers: awsHeaders, | ||
payload: .none | ||
) | ||
|
||
let request = Request(using: container) | ||
request.http.method = .PUT | ||
request.http.headers = headers | ||
request.http.body = .empty | ||
request.http.url = destinationUrl | ||
|
||
let client = try container.make(Client.self) | ||
return client.send(request) | ||
.map { | ||
try self.check($0) | ||
return try $0.decode(to: File.CopyResponse.self) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters