-
Notifications
You must be signed in to change notification settings - Fork 648
/
GCStorageStream.scala
325 lines (286 loc) · 13.8 KB
/
GCStorageStream.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.stream.alpakka.googlecloud.storage.impl
import akka.annotation.InternalApi
import akka.dispatch.ExecutionContexts
import akka.dispatch.ExecutionContexts.parasitic
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model.HttpMethods.{DELETE, POST}
import akka.http.scaladsl.model.Uri.{Path, Query}
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, FromResponseUnmarshaller, Unmarshal, Unmarshaller}
import akka.stream.alpakka.google._
import akka.stream.alpakka.google.auth.{Credentials, ServiceAccountCredentials}
import akka.stream.alpakka.google.http.GoogleHttp
import akka.stream.alpakka.google.implicits._
import akka.stream.alpakka.google.scaladsl.{`X-Upload-Content-Type`, Paginated}
import akka.stream.alpakka.googlecloud.storage._
import akka.stream.alpakka.googlecloud.storage.impl.Formats._
import akka.stream.scaladsl.{Flow, Keep, RunnableGraph, Sink, Source}
import akka.stream.{Attributes, Materializer}
import akka.util.ByteString
import akka.{Done, NotUsed}
import com.github.ghik.silencer.silent
import spray.json._
import scala.concurrent.Future
@InternalApi private[storage] object GCStorageStream {
private val baseUrl = "https://storage.googleapis.com/"
private val basePath = "/storage/v1"
def getBucketSource(bucketName: String): Source[Option[Bucket], NotUsed] = {
val uri = Uri(baseUrl).withPath(Path(basePath) ++ getBucketPath(bucketName))
val request = HttpRequest(uri = uri)
makeRequestSource[Option[Bucket]](request)
}
def getBucket(bucketName: String)(implicit mat: Materializer, attr: Attributes): Future[Option[Bucket]] =
getBucketSource(bucketName).withAttributes(attr).runWith(Sink.head)
def createBucketSource(bucketName: String, location: String): Source[Bucket, NotUsed] = source { settings =>
val uri = Uri(baseUrl).withPath(Path(basePath) / "b").withQuery(Query("project" -> settings.projectId))
implicit val ec = parasitic
val request = Marshal(BucketInfo(bucketName, location)).to[RequestEntity].map { entity =>
HttpRequest(POST, uri, entity = entity)
}
makeRequestSource[Bucket](request)
}
def createBucket(bucketName: String, location: String)(implicit mat: Materializer, attr: Attributes): Future[Bucket] =
createBucketSource(bucketName, location).withAttributes(attr).runWith(Sink.head)
def deleteBucketSource(bucketName: String): Source[Done, NotUsed] = source { settings =>
val uri = Uri(baseUrl).withPath(Path(basePath) ++ getBucketPath(bucketName))
val request = HttpRequest(DELETE, uri)
makeRequestSource[Done](request)
}
def deleteBucket(bucketName: String)(implicit mat: Materializer, attr: Attributes): Future[Done] =
deleteBucketSource(bucketName).withAttributes(attr).runWith(Sink.head)
def listBucket(bucket: String, prefix: Option[String], versions: Boolean = false): Source[StorageObject, NotUsed] = {
val query = ("versions" -> versions.toString) +: ("prefix" -> prefix) ?+: Query.Empty
val uri = Uri(baseUrl).withPath(Path(basePath) ++ getBucketPath(bucket) / "o").withQuery(query)
val request = HttpRequest(uri = uri)
implicit val paginated: Paginated[Option[BucketListResult]] = _.flatMap(_.nextPageToken)
PaginatedRequest[Option[BucketListResult]](request).mapConcat(_.fold(List.empty[StorageObject])(_.items))
}
def getObject(bucket: String,
objectName: String,
generation: Option[Long] = None): Source[Option[StorageObject], NotUsed] = {
val uri = Uri(baseUrl)
.withPath(Path(basePath) ++ getObjectPath(bucket, objectName))
.withQuery(Query(generation.map("generation" -> _.toString).toMap))
val request = HttpRequest(uri = uri)
makeRequestSource[Option[StorageObject]](request)
}
def deleteObjectSource(bucket: String,
objectName: String,
generation: Option[Long] = None): Source[Boolean, NotUsed] = {
val uri = Uri(baseUrl)
.withPath(Path(basePath) ++ getObjectPath(bucket, objectName))
.withQuery(Query(generation.map("generation" -> _.toString).toMap))
val request = HttpRequest(DELETE, uri)
makeRequestSource[Option[Done]](request).map(_.isDefined)
}
def deleteObjectsByPrefixSource(bucket: String, prefix: Option[String]): Source[Boolean, NotUsed] =
listBucket(bucket, prefix)
.flatMapConcat(listBucketResult => deleteObjectSource(bucket, listBucketResult.name))
def putObject(bucket: String,
objectName: String,
data: Source[ByteString, _],
contentType: ContentType): Source[StorageObject, NotUsed] = {
val uri = Uri(baseUrl)
.withPath(Path("/upload" + basePath) ++ getBucketPath(bucket) / "o")
.withQuery(Query("uploadType" -> "media", "name" -> objectName))
val entity = HttpEntity(contentType, data)
val request = HttpRequest(POST, uri, entity = entity)
makeRequestSource[StorageObject](request)
}
def download(bucket: String,
objectName: String,
generation: Option[Long] = None): Source[Option[Source[ByteString, NotUsed]], NotUsed] = {
val query = ("alt" -> "media") +: ("generation" -> generation.map(_.toString)) ?+: Query.Empty
val uri = Uri(baseUrl)
.withPath(Path(basePath) ++ getObjectPath(bucket, objectName))
.withQuery(query)
val request = HttpRequest(uri = uri)
implicit val um: Unmarshaller[HttpEntity, Source[ByteString, NotUsed]] =
Unmarshaller.strict(_.withoutSizeLimit.dataBytes.mapMaterializedValue(_ => NotUsed))
makeRequestSource[Option[Source[ByteString, NotUsed]]](request)
}
def resumableUpload(bucket: String,
objectName: String,
contentType: ContentType,
chunkSize: Int = 5 * 1024 * 1024,
metadata: Option[Map[String, String]] = None): Sink[ByteString, Future[StorageObject]] =
Sink
.fromMaterializer { (mat, attr) =>
implicit val settings = {
val s = resolveSettings(mat, attr)
s.copy(requestSettings = s.requestSettings.copy(uploadChunkSize = chunkSize))
}
val uri = Uri(baseUrl)
.withPath(Path("/upload" + basePath) ++ getBucketPath(bucket) / "o")
.withQuery(("uploadType" -> "resumable") +: ("name" -> objectName) +: Query.Empty)
val headers = List(`X-Upload-Content-Type`(contentType))
val entity =
metadata.fold(HttpEntity.Empty)(m => HttpEntity(ContentTypes.`application/json`, m.toJson.toString))
val request = HttpRequest(POST, uri, headers, entity)
implicit val um: Unmarshaller[HttpResponse, StorageObject] = Unmarshaller.withMaterializer {
implicit ec => implicit mat =>
{
case HttpResponse(status, _, entity, _) if status.isSuccess() =>
Unmarshal(entity).to[StorageObject]
case HttpResponse(status, _, entity, _) =>
Unmarshal(entity).to[String].flatMap { errorString =>
Future.failed(new RuntimeException(s"Uploading part failed with status $status: $errorString"))
}
}: PartialFunction[HttpResponse, Future[StorageObject]]
}.withDefaultRetry
// Workaround for https://github.com/akka/akka/issues/30141
// Sink.fromGraph(ResumableUpload[StorageObject](request)).addAttributes(GoogleAttributes.settings(settings))
Flow
.fromSinkAndSourceMat(
ResumableUpload[StorageObject](request).addAttributes(GoogleAttributes.settings(settings)),
Source.empty[Nothing]
)(Keep.left)
.to(Sink.ignore)
}
.mapMaterializedValue(_.flatten)
def rewrite(sourceBucket: String,
sourceObjectName: String,
destinationBucket: String,
destinationObjectName: String): RunnableGraph[Future[StorageObject]] = {
sealed trait RewriteState
case object Starting extends RewriteState
case class Running(rewriteToken: String) extends RewriteState
case object Finished extends RewriteState
val sourcePath = getObjectPath(sourceBucket, sourceObjectName)
val destinationPath = getObjectPath(destinationBucket, destinationObjectName)
def rewriteRequest(rewriteToken: Option[String]): Source[Option[(RewriteState, RewriteResponse)], NotUsed] = {
val query = ("rewriteToken" -> rewriteToken) ?+: Query.Empty
val uri = Uri(baseUrl)
.withPath(Path(basePath) ++ sourcePath / "rewriteTo" ++ destinationPath)
.withQuery(query)
val entity = HttpEntity.Empty.withoutSizeLimit()
val request = HttpRequest(POST, uri, entity = entity)
makeRequestSource[RewriteResponse](request).map { rewriteResponse =>
Some(
rewriteResponse.rewriteToken.fold[(RewriteState, RewriteResponse)]((Finished, rewriteResponse))(
token => (Running(token), rewriteResponse)
)
)
}
}
Source
.fromMaterializer { (mat, attr) =>
Source
.unfoldAsync[RewriteState, RewriteResponse](Starting) {
case Finished => Future.successful(None)
case Starting => rewriteRequest(None).runWith(Sink.head)(mat)
case Running(token) => rewriteRequest(Some(token)).runWith(Sink.head)(mat)
}
}
.toMat(Sink.last[RewriteResponse])(Keep.right)
.mapMaterializedValue(
_.flatMap(
_.resource match {
case Some(resource) => Future.successful(resource)
case None => Future.failed(new RuntimeException("Storage object is missing"))
}
)(ExecutionContexts.parasitic)
)
}
private def makeRequestSource[T: FromResponseUnmarshaller](request: HttpRequest): Source[T, NotUsed] =
makeRequestSource[T](Future.successful(request))
private def makeRequestSource[T: FromResponseUnmarshaller](request: Future[HttpRequest]): Source[T, NotUsed] =
Source
.fromMaterializer { (mat, attr) =>
implicit val settings = resolveSettings(mat, attr)
Source.lazyFuture { () =>
request.flatMap { request =>
GoogleHttp()(mat.system).singleAuthenticatedRequest[T](request)
}(ExecutionContexts.parasitic)
}
}
.mapMaterializedValue(_ => NotUsed)
private def getBucketPath(bucket: String) =
Path("/b") / bucket
private def getObjectPath(bucket: String, objectName: String) =
getBucketPath(bucket) / "o" / objectName
implicit def unmarshaller[T: FromEntityUnmarshaller]: Unmarshaller[HttpResponse, T] =
Unmarshaller.withMaterializer { implicit ec => implicit mat => response: HttpResponse =>
response match {
case HttpResponse(status, _, entity, _) if status.isSuccess() && !status.isRedirection() =>
Unmarshal(entity).to[T]
case HttpResponse(status, _, entity, _) =>
Unmarshal(entity).to[String].flatMap { err =>
Future.failed(new RuntimeException(s"[${status.intValue}] $err"))
}
}
}.withDefaultRetry
implicit def optionUnmarshaller[T: FromEntityUnmarshaller]: Unmarshaller[HttpResponse, Option[T]] =
Unmarshaller.withMaterializer { implicit ec => implicit mat => response: HttpResponse =>
response match {
case HttpResponse(status, _, entity, _) if status.isSuccess() && !status.isRedirection() =>
Unmarshal(entity).to[T].map(Some(_))
case HttpResponse(StatusCodes.NotFound, _, entity, _) =>
entity.discardBytes()
Future.successful(None)
case HttpResponse(status, _, entity, _) =>
Unmarshal(entity).to[String].flatMap { err =>
Future.failed(new RuntimeException(s"[${status.intValue}] $err"))
}
}
}.withDefaultRetry
implicit private val doneUnmarshaller: Unmarshaller[HttpEntity, Done] =
Unmarshaller.withMaterializer { _ => implicit mat => entity =>
entity.discardBytes().future
}
private def source[T](f: GoogleSettings => Source[T, NotUsed]): Source[T, NotUsed] =
Source
.fromMaterializer { (mat, attr) =>
f(resolveSettings(mat, attr))
}
.mapMaterializedValue(_ => NotUsed)
@silent("deprecated")
private def resolveSettings(mat: Materializer, attr: Attributes) = {
implicit val sys = mat.system
val legacySettings = attr
.get[GCStorageSettingsValue]
.map(_.settings)
.getOrElse {
val configPath = attr.get[GCStorageSettingsPath](GCStorageSettingsPath.Default).path
GCStorageExt(sys).settings(configPath)
}
val settings = GoogleAttributes.resolveSettings(mat, attr)
if (legacySettings.privateKey == "deprecated")
GoogleAttributes.resolveSettings(mat, attr)
else {
sys.log.warning("Configuration via alpakka.google.cloud.storage is deprecated")
require(
(legacySettings.baseUrl.contains("googleapis.com") || legacySettings.baseUrl == "unsupported")
&& (legacySettings.basePath.contains("storage/v1") || legacySettings.basePath == "unsupported")
&& (legacySettings.tokenUrl.contains("googleapis.com") || legacySettings.tokenUrl == "unsupported"),
"Non-default base-url/base-path/token-url no longer supported, use config path alpakka.google.forward-proxy"
)
val legacyScopes = legacySettings.tokenScope.split(" ").toList
val credentials = Credentials.cache(
(
legacySettings.projectId,
legacySettings.clientEmail,
legacySettings.privateKey,
legacyScopes,
mat.system.name
)
) {
ServiceAccountCredentials(
legacySettings.projectId,
legacySettings.clientEmail,
legacySettings.privateKey,
legacyScopes
)
}
GoogleSettings(
legacySettings.projectId,
credentials,
settings.requestSettings
)
}
}
}