|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using BaGet.Core.Services; |
| 9 | +using BaGet.GCP.Configuration; |
| 10 | +using Google; |
| 11 | +using Google.Cloud.Storage.V1; |
| 12 | +using Microsoft.Extensions.Options; |
| 13 | + |
| 14 | +namespace BaGet.GCP.Services |
| 15 | +{ |
| 16 | + public class GoogleCloudStorageService : IStorageService |
| 17 | + { |
| 18 | + private readonly string _bucketName; |
| 19 | + |
| 20 | + public GoogleCloudStorageService(IOptionsSnapshot<GoogleCloudStorageOptions> options) |
| 21 | + { |
| 22 | + if (options == null) |
| 23 | + throw new ArgumentNullException(nameof(options)); |
| 24 | + |
| 25 | + _bucketName = options.Value.BucketName; |
| 26 | + } |
| 27 | + |
| 28 | + public async Task<Stream> GetAsync(string path, CancellationToken cancellationToken = default) |
| 29 | + { |
| 30 | + using (var storage = await StorageClient.CreateAsync()) |
| 31 | + { |
| 32 | + var stream = new MemoryStream(); |
| 33 | + await storage.DownloadObjectAsync(_bucketName, CoercePath(path), stream, cancellationToken: cancellationToken); |
| 34 | + stream.Position = 0; |
| 35 | + return stream; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public Task<Uri> GetDownloadUriAsync(string path, CancellationToken cancellationToken = default) |
| 40 | + { |
| 41 | + // returns an Authenticated Browser Download URL: https://cloud.google.com/storage/docs/request-endpoints#cookieauth |
| 42 | + return Task.FromResult(new Uri($"https://storage.googleapis.com/{_bucketName}/{CoercePath(path).TrimStart('/')}")); |
| 43 | + } |
| 44 | + |
| 45 | + public async Task<PutResult> PutAsync(string path, Stream content, string contentType, CancellationToken cancellationToken = default) |
| 46 | + { |
| 47 | + using (var storage = await StorageClient.CreateAsync()) |
| 48 | + using (var seekableContent = new MemoryStream()) |
| 49 | + { |
| 50 | + await content.CopyToAsync(seekableContent, 65536, cancellationToken); |
| 51 | + seekableContent.Position = 0; |
| 52 | + |
| 53 | + var objectName = CoercePath(path); |
| 54 | + |
| 55 | + try |
| 56 | + { |
| 57 | + // attempt to upload, succeeding only if the object doesn't exist |
| 58 | + await storage.UploadObjectAsync(_bucketName, objectName, contentType, seekableContent, new UploadObjectOptions { IfGenerationMatch = 0 }, cancellationToken); |
| 59 | + return PutResult.Success; |
| 60 | + } |
| 61 | + catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.PreconditionFailed) |
| 62 | + { |
| 63 | + // the object already exists; get the hash of its content from its metadata |
| 64 | + var existingObject = await storage.GetObjectAsync(_bucketName, objectName, cancellationToken: cancellationToken); |
| 65 | + var existingHash = Convert.FromBase64String(existingObject.Md5Hash); |
| 66 | + |
| 67 | + // hash the content that was uploaded |
| 68 | + seekableContent.Position = 0; |
| 69 | + byte[] contentHash; |
| 70 | + using (var md5 = MD5.Create()) |
| 71 | + contentHash = md5.ComputeHash(seekableContent); |
| 72 | + |
| 73 | + // conflict if the two hashes are different |
| 74 | + return existingHash.SequenceEqual(contentHash) ? PutResult.AlreadyExists : PutResult.Conflict; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public async Task DeleteAsync(string path, CancellationToken cancellationToken = default) |
| 80 | + { |
| 81 | + using (var storage = await StorageClient.CreateAsync()) |
| 82 | + { |
| 83 | + try |
| 84 | + { |
| 85 | + var obj = await storage.GetObjectAsync(_bucketName, CoercePath(path), cancellationToken: cancellationToken); |
| 86 | + await storage.DeleteObjectAsync(obj, cancellationToken: cancellationToken); |
| 87 | + } |
| 88 | + catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.NotFound) |
| 89 | + { |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static string CoercePath(string path) |
| 95 | + { |
| 96 | + // although Google Cloud Storage objects exist in a flat namespace, using forward slashes allows the objects to |
| 97 | + // be exposed as nested subdirectories, e.g., when browsing via Google Cloud Console |
| 98 | + return path.Replace('\\', '/'); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments