Skip to content

Commit a794c0b

Browse files
authored
Update README.md
1 parent 83686e3 commit a794c0b

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,174 @@ let s3 = try req.makeS3Signer() // or req.make(S3Signer.self)
2929
s3.headers(...)
3030
```
3131

32+
### Available methods
33+
34+
```swift
35+
/// S3 client Protocol
36+
public protocol S3Client: Service {
37+
38+
/// Get list of objects
39+
func buckets(on: Container) throws -> Future<BucketsInfo>
40+
41+
/// Create a bucket
42+
func create(bucket: String, region: Region?, on container: Container) throws -> Future<Void>
43+
44+
/// Delete a bucket wherever it is (WIP)
45+
// func delete(bucket: String, on container: Container) throws -> Future<Void>
46+
47+
/// Delete a bucket
48+
func delete(bucket: String, region: Region?, on container: Container) throws -> Future<Void>
49+
50+
/// Get bucket location (WIP)
51+
// func location(bucket: String, on container: Container) throws -> Future<Bucket.Location>
52+
53+
/// Get list of objects
54+
func list(bucket: String, region: Region?, on container: Container) throws -> Future<BucketResults>
55+
56+
/// Get list of objects
57+
func list(bucket: String, region: Region?, headers: [String: String], on container: Container) throws -> Future<BucketResults>
58+
59+
/// Upload file to S3
60+
func put(file: File.Upload, headers: [String: String], on: Container) throws -> EventLoopFuture<File.Response>
61+
62+
/// Upload file to S3
63+
func put(file url: URL, destination: String, access: AccessControlList, on: Container) throws -> Future<File.Response>
64+
65+
/// Upload file to S3
66+
func put(file url: URL, destination: String, bucket: String?, access: AccessControlList, on: Container) throws -> Future<File.Response>
67+
68+
/// Upload file to S3
69+
func put(file path: String, destination: String, access: AccessControlList, on: Container) throws -> Future<File.Response>
70+
71+
/// Upload file to S3
72+
func put(file path: String, destination: String, bucket: String?, access: AccessControlList, on: Container) throws -> Future<File.Response>
73+
74+
/// Upload file to S3
75+
func put(string: String, destination: String, on: Container) throws -> Future<File.Response>
76+
77+
/// Upload file to S3
78+
func put(string: String, destination: String, access: AccessControlList, on: Container) throws -> Future<File.Response>
79+
80+
/// Upload file to S3
81+
func put(string: String, mime: MediaType, destination: String, on: Container) throws -> Future<File.Response>
82+
83+
/// Upload file to S3
84+
func put(string: String, mime: MediaType, destination: String, access: AccessControlList, on: Container) throws -> Future<File.Response>
85+
86+
/// Upload file to S3
87+
func put(string: String, mime: MediaType, destination: String, bucket: String?, access: AccessControlList, on: Container) throws -> Future<File.Response>
88+
89+
/// Retrieve file data from S3
90+
func get(fileInfo file: LocationConvertible, on container: Container) throws -> Future<File.Info>
91+
92+
/// Retrieve file data from S3
93+
func get(fileInfo file: LocationConvertible, headers: [String: String], on container: Container) throws -> Future<File.Info>
94+
95+
/// Retrieve file data from S3
96+
func get(file: LocationConvertible, on: Container) throws -> Future<File.Response>
97+
98+
/// Retrieve file data from S3
99+
func get(file: LocationConvertible, headers: [String: String], on: Container) throws -> Future<File.Response>
100+
101+
/// Delete file from S3
102+
func delete(file: LocationConvertible, on: Container) throws -> Future<Void>
103+
104+
/// Delete file from S3
105+
func delete(file: LocationConvertible, headers: [String: String], on: Container) throws -> Future<Void>
106+
}
107+
```
108+
109+
### Example usage
110+
111+
```swift
112+
public func routes(_ router: Router) throws {
113+
114+
// Get all available buckets
115+
router.get("buckets") { req -> Future<BucketsInfo> in
116+
let s3 = try req.makeS3Client()
117+
return try s3.buckets(on: req)
118+
}
119+
120+
// Create new bucket
121+
router.put("bucket") { req -> Future<String> in
122+
let s3 = try req.makeS3Client()
123+
return try s3.create(bucket: "api-created-bucket", region: .euCentral1, on: req).map(to: String.self) {
124+
return ":)"
125+
}.catchMap({ (error) -> (String) in
126+
if let error = error.s3ErroMessage() {
127+
return error.message
128+
}
129+
return ":("
130+
}
131+
)
132+
}
133+
134+
// Delete bucket
135+
router.delete("bucket") { req -> Future<String> in
136+
let s3 = try req.makeS3Client()
137+
return try s3.delete(bucket: "api-created-bucket", region: .euCentral1, on: req).map(to: String.self) {
138+
return ":)"
139+
}.catchMap({ (error) -> (String) in
140+
if let error = error.s3ErroMessage() {
141+
return error.message
142+
}
143+
return ":("
144+
}
145+
)
146+
}
147+
148+
// Get list of objects
149+
router.get("files") { req -> Future<BucketResults> in
150+
let s3 = try req.makeS3Client()
151+
return try s3.list(bucket: "booststore", region: .usEast1, headers: [:], on: req).catchMap({ (error) -> (BucketResults) in
152+
if let error = error.s3ErroMessage() {
153+
print(error.message)
154+
}
155+
throw error
156+
})
157+
}
158+
159+
// Demonstrate work with files
160+
router.get("files/test") { req -> Future<String> in
161+
let string = "Content of my example file"
162+
163+
let fileName = "file-hu.txt"
164+
165+
let s3 = try req.makeS3Client()
166+
do {
167+
return try s3.put(string: string, destination: fileName, access: .publicRead, on: req).flatMap(to: String.self) { putResponse in
168+
print("PUT response:")
169+
print(putResponse)
170+
return try s3.get(file: fileName, on: req).flatMap(to: String.self) { getResponse in
171+
print("GET response:")
172+
print(getResponse)
173+
print(String(data: getResponse.data, encoding: .utf8) ?? "Unknown content!")
174+
return try s3.get(fileInfo: fileName, on: req).flatMap(to: String.self) { infoResponse in
175+
print("HEAD/Info response:")
176+
print(infoResponse)
177+
return try s3.delete(file: fileName, on: req).map() { response in
178+
print("DELETE response:")
179+
print(response)
180+
let json = try JSONEncoder().encode(infoResponse)
181+
return String(data: json, encoding: .utf8) ?? "Unknown content!"
182+
}.catchMap({ error -> (String) in
183+
if let error = error.s3ErroMessage() {
184+
return error.message
185+
}
186+
return ":("
187+
}
188+
)
189+
}
190+
}
191+
}
192+
} catch {
193+
print(error)
194+
fatalError()
195+
}
196+
}
197+
}
198+
```
199+
32200
## Support
33201

34202
Join our [Slack](http://bit.ly/2B0dEyt), channel <b>#help-boost</b> to ... well, get help :)

0 commit comments

Comments
 (0)