Skip to content

Commit 100c049

Browse files
authored
Merge pull request #28 from SusanDoggie/stream-upload
Upload file by chunked
2 parents e3a18b3 + b6b8aed commit 100c049

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

Storage/Sources/API/StorageObjectAPI.swift

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import NIO
99
import NIOHTTP1
10+
import AsyncHTTPClient
1011
import Foundation
1112

1213
public protocol StorageObjectAPI {
@@ -58,6 +59,18 @@ public protocol StorageObjectAPI {
5859
/// - Parameter queryParameters: [Optional query parameters](https://cloud.google.com/storage/docs/json_api/v1/objects/get#parameters)
5960
func getMedia(bucket: String, object: String, range: ClosedRange<Int>?, queryParameters: [String: String]?) -> EventLoopFuture<GoogleCloudStorgeDataResponse>
6061

62+
/// Stores a new object with no metadata.
63+
/// - Parameter bucket: Name of the bucket in which to store the new object. Overrides the provided object metadata's bucket value, if any.
64+
/// - Parameter body: The content to be uploaded to a bucket.
65+
/// - Parameter name: The name of the object. Required if not specified by URL parameter.
66+
/// - Parameter contentType: Content-Type of the object data. If an object is stored without a Content-Type, it is served as application/octet-stream.
67+
/// - Parameter queryParameters: [Optional query parameters](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#parameters)
68+
func createSimpleUpload(bucket: String,
69+
body: HTTPClient.Body,
70+
name: String,
71+
contentType: String,
72+
queryParameters: [String: String]?) -> EventLoopFuture<GoogleCloudStorageObject>
73+
6174
/// Stores a new object with no metadata.
6275
/// - Parameter bucket: Name of the bucket in which to store the new object. Overrides the provided object metadata's bucket value, if any.
6376
/// - Parameter data: The content to be uploaded to a bucket.
@@ -193,13 +206,25 @@ extension StorageObjectAPI {
193206
return getMedia(bucket: bucket, object: object, range: range, queryParameters: queryParameters)
194207
}
195208

209+
public func createSimpleUpload(bucket: String,
210+
body: HTTPClient.Body,
211+
name: String,
212+
contentType: String,
213+
queryParameters: [String: String]? = nil) -> EventLoopFuture<GoogleCloudStorageObject> {
214+
return createSimpleUpload(bucket: bucket,
215+
body: body,
216+
name: name,
217+
contentType: contentType,
218+
queryParameters: queryParameters)
219+
}
220+
196221
public func createSimpleUpload(bucket: String,
197222
data: Data,
198223
name: String,
199224
contentType: String,
200225
queryParameters: [String: String]? = nil) -> EventLoopFuture<GoogleCloudStorageObject> {
201226
return createSimpleUpload(bucket: bucket,
202-
data: data,
227+
body: .data(data),
203228
name: name,
204229
contentType: contentType,
205230
queryParameters: queryParameters)
@@ -373,7 +398,7 @@ public final class GoogleCloudStorageObjectAPI: StorageObjectAPI {
373398
}
374399

375400
public func createSimpleUpload(bucket: String,
376-
data: Data,
401+
body: HTTPClient.Body,
377402
name: String,
378403
contentType: String,
379404
queryParameters: [String: String]?) -> EventLoopFuture<GoogleCloudStorageObject> {
@@ -385,10 +410,14 @@ public final class GoogleCloudStorageObjectAPI: StorageObjectAPI {
385410
} else {
386411
queryParams = "uploadType=media&name=\(name)"
387412
}
388-
389-
let headers: HTTPHeaders = ["Content-Type": contentType]
390413

391-
return request.send(method: .POST, headers: headers, path: "\(uploadEndpoint)/\(bucket)/o", query: queryParams, body: .data(data))
414+
var headers: HTTPHeaders = ["Content-Type": contentType]
415+
416+
if body.length == nil {
417+
headers.add(name: "Transfer-Encoding", value: "chunked")
418+
}
419+
420+
return request.send(method: .POST, headers: headers, path: "\(uploadEndpoint)/\(bucket)/o", query: queryParams, body: body)
392421
}
393422

394423
public func list(bucket: String, queryParameters: [String: String]?) -> EventLoopFuture<StorageObjectList> {

0 commit comments

Comments
 (0)