Skip to content

Commit

Permalink
Merge pull request #22 from zdnk/s3-copy-file
Browse files Browse the repository at this point in the history
Copy file API
  • Loading branch information
rafiki270 authored Oct 23, 2018
2 parents bd17449 + f8cda7f commit e4a9c4a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
42 changes: 42 additions & 0 deletions Sources/S3/Extensions/S3+Copy.swift
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)
}
}

}
16 changes: 16 additions & 0 deletions Sources/S3/Models/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ public struct File {

}

/// Copy file response comming back from S3
public struct CopyResponse: Content {

/// ETag
public let etag: String

/// Last modified
public let modified: Date

enum CodingKeys: String, CodingKey {
case etag = "ETag"
case modified = "LastModified"
}

}

/// File info response comming back from S3
public struct Info: Content {

Expand Down
12 changes: 12 additions & 0 deletions Sources/S3/Protocols/S3Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ public protocol S3Client: Service {

/// Delete file from S3
func delete(file: LocationConvertible, headers: [String: String], on: Container) throws -> Future<Void>

/// Copy file on S3
func copy(file: LocationConvertible, to: LocationConvertible, headers: [String: String], on: Container) throws -> Future<File.CopyResponse>
}

extension S3Client {

/// Copy file on S3
public func copy(file: LocationConvertible, to: LocationConvertible, on container: Container) throws -> Future<File.CopyResponse> {
return try self.copy(file: file, to: to, headers: [:], on: container)
}

}
2 changes: 1 addition & 1 deletion Sources/S3/S3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import HTTP


/// Main S3 class
public class S3: S3Client {
public class S3: S3Client {

/// Error messages
public enum Error: Swift.Error {
Expand Down

0 comments on commit e4a9c4a

Please sign in to comment.