async Task <IStreamConsumer> CreateUploadConsumerAsync(Uri uri, string?overrideContentType, CancellationToken cancellationToken) { var client = CreateClient(); try { var contentType = Choose2(overrideContentType, ContentType, "application/octet-stream"); var bucketName = uri.Host; var name = uri.AbsolutePath.Trim('/'); var predefinedAcl = IsPublic ? PredefinedAcls.Public : PredefinedAcls.Private; var initEndpoint = CreateInitEndpoint(bucketName, predefinedAcl); var token = await Credential.GetAccessTokenAsync(GoogleStorageCredential.ReadWriteScopes, cancellationToken).ConfigureAwait(false); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var gobj = new GoogleObjectData { ContentType = contentType, CacheControl = CacheControl, Name = name }; Logger.LogInformation( "Initializing GCS upload to gs://{0}/{1} with [Content-Type = {2}, CacheControl = {3}, PredefinedAcl = {4}].", bucketName, name, contentType, CacheControl, predefinedAcl ); var content = new ByteArrayContent(JsonSerializer.SerializeToUtf8Bytes(gobj)); content.Headers.ContentType = _jsonContentType; using var request = new HttpRequestMessage(HttpMethod.Post, initEndpoint) { Content = content }; request.Headers.Add("X-Upload-Content-Type", contentType); using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var uploadEndpoint = response.Headers.Location ?? throw new GoogleCloudStorageUploadException("Resumable upload response contans no location."); return(new GoogleCloudStorageUploaderConsumer(client, uploadEndpoint, contentType)); } catch (Exception) { // if no GoogleCloudStorageUploaderConsumer is created client should be disposed client.Dispose(); throw; } }
public async Task CopyAsync( string sourceBucket, string sourceName, string destinationBucket, string destinationName, string contentType, string?cacheControl = default, bool isPublic = true, string?accessToken = default, CancellationToken cancellationToken = default) { var requestUri = $"https://www.googleapis.com/storage/v1/b/{sourceBucket}/o/{Uri.EscapeDataString(sourceName)}/copyTo/b/{destinationBucket}/o/{Uri.EscapeDataString(destinationName)}"; using var client = CreateHttpClient(); using var request = new HttpRequestMessage(HttpMethod.Post, requestUri); var obj = new GoogleObjectData { ContentType = contentType, CacheControl = cacheControl ?? DefaultCacheControl }; if (isPublic) { obj.Acl.Add(new GoogleAccessControlEntry { Entity = "allUsers", Role = "READER" }); } request.Content = JsonContent.Create(obj, _jsonMediaType, _jsonOptions); if (!string.IsNullOrEmpty(accessToken)) { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } request.SetRequiredGSCScope(obj.Acl.Count > 0 ? FullControlScope : ReadWriteScope); using var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken) .ConfigureAwait(false); response.EnsureSuccessStatusCode(); }