Beispiel #1
0
        public async Task <IActionResult> Index()
        {
            var model = new HomeIndex();

            if (new string[] { null, "", "your-google-bucket-name" }
                .Contains(_options.BucketName))
            {
                model.MissingBucketName = true;
                return(View(model));
            }
            try {
                var storageObject =
                    await _storage.GetObjectAsync(_options.BucketName, _options.ObjectName);

                model.MediaLink = storageObject.MediaLink;
                MemoryStream m = new MemoryStream();
                await _storage.DownloadObjectAsync(
                    _options.BucketName, _options.ObjectName, m);

                m.Seek(0, SeekOrigin.Begin);
                byte[] content = new byte[m.Length];
                m.Read(content, 0, content.Length);
                model.Content = Encoding.UTF8.GetString(content);
            }
            catch (GoogleApiException e)
                when(e.HttpStatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    // Does not exist yet.  No problem.
                }
            return(View(model));
        }
        private async Task <BlobData> GetLatestDataAsync(StorageClient storageClient)
        {
            // Set the appropriate AccessCondition based on what we believe the latest
            // file contents to be, then make the request.

            var latestCachedData = Volatile.Read(ref _cachedBlobData); // local ref so field isn't mutated under our feet

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    var obj = await storageClient.GetObjectAsync(_bucketName, _name);

                    await storageClient.DownloadObjectAsync(obj, memoryStream);

                    latestCachedData = new BlobData()
                    {
                        BlobContents = memoryStream.ToArray(),
                        ETag         = obj.ETag
                    };
                }
                Volatile.Write(ref _cachedBlobData, latestCachedData);
            }
            catch
            {
                latestCachedData = null;
                Volatile.Write(ref _cachedBlobData, latestCachedData);
            }

            return(latestCachedData);
        }
Beispiel #3
0
 public ConfiguredTaskAwaitable <Object> GetObjectAsync(string fileName)
 {
     return(_client.GetObjectAsync(
                _bucketName,
                fileName,
                cancellationToken: CancellationToken
                ).ConfigureAwait(false));
 }
Beispiel #4
0
 public Task <Object> GetObjectAsync(string fileName)
 {
     return(_client.GetObjectAsync(
                _bucketName,
                fileName,
                cancellationToken: CancellationToken
                ));
 }
Beispiel #5
0
        private static async Task DeleteDocumentObject(StorageClient storage, string bucket, string remoteFileName)
        {
            using (new Timing($"Delete {bucket}:{remoteFileName}"))
            {
                var document = await storage.GetObjectAsync(bucket, remoteFileName);

                await storage.DeleteObjectAsync(document);
            }
        }
        public async Task RenameFileAsync(string bucket, string objectName, string filename)
        {
            using (StorageClient client = await StorageClient.CreateAsync(_credential))
            {
                var dataObject = await client.GetObjectAsync(bucket, objectName);

                dataObject.ContentDisposition = $"filename=\"{filename}\"";

                await client.PatchObjectAsync(dataObject);
            }
        }
        protected override async Task <Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken)
        {
            fullPath = StoragePath.Normalize(fullPath);

            try
            {
                Object obj = await _client.GetObjectAsync(_bucketName, fullPath,
                                                          new GetObjectOptions
                {
                    //todo
                },
                                                          cancellationToken).ConfigureAwait(false);

                return(GConvert.ToBlob(obj));
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }
        }
Beispiel #8
0
        public async Task <IActionResult> GetWithBranch(string branch, string version, string filename)
        {
            using (var stream = new MemoryStream())
            {
                await storageClient.DownloadObjectAsync("upward-test", $"pkg/{Response.Headers["x-project-id"]}/{branch}/{version}/{filename}", stream);

                var metaData = await storageClient.GetObjectAsync("upward-test", $"pkg/{Response.Headers["x-project-id"]}/{branch}/{version}/{filename}");

                return(File(stream.ToArray(), metaData.ContentType));
            }
        }
        /// <summary>
        /// Reads the metadata for the object in Google Cloud Storage
        /// </summary>
        /// <param name="objectName">Name of the Google Cloud object</param>
        /// <param name="addContentLength">True to add in the content length metadata</param>
        /// <returns>Metadata for the object</returns>
        private async Task <Dictionary <string, string> > GetObjectMetadataAsync(string objectName, bool addContentLength)
        {
            var o = await _storageClient.GetObjectAsync(_options.BucketName, objectName);

            var metadata = new Dictionary <string, string>(o.Metadata);

            if (addContentLength)
            {
                metadata[MetadataKeys.Length] = o.Size.ToString();
            }
            return(metadata);
        }
        /// <summary>
        /// Function to stream the download the results of a bucket as a stream
        /// </summary>
        /// <param name="client">Google storage client</param>
        /// <param name="bucketName">Name of the bucket</param>
        /// <param name="objectName">Object name to download</param>
        /// <returns>Open stream for reading the data</returns>
        internal static async Task <Stream> DownloadAsStreamAsync(StorageClient client, string bucketName, string objectName)
        {
            var o = await client.GetObjectAsync(bucketName, objectName);

            var request = new HttpRequestMessage
            {
                RequestUri = new Uri(o.MediaLink)
            };
            var response = await client.Service.HttpClient.SendAsync(request);

            return(await response.Content.ReadAsStreamAsync());
        }
        public async Task <Game> GetGame(string name)
        {
            var source = await _storageClient.GetObjectAsync(BucketName, name);

            using (var ms = new MemoryStream())
            {
                await _storageClient.DownloadObjectAsync(source, ms);

                var json = Encoding.UTF8.GetString(ms.ToArray());

                return(JsonConvert.DeserializeObject <Game>(json));
            }
        }
Beispiel #12
0
        public async Task <BlobDescriptor> GetBlobDescriptorAsync(string containerName, string blobName)
        {
            try
            {
                var obj = await _client.GetObjectAsync(_bucket, ObjectName(containerName, blobName),
                                                       new GetObjectOptions { Projection = Projection.Full });

                return(GetBlobDescriptor(obj));
            }
            catch (GoogleApiException gae)
            {
                throw Error(gae);
            }
        }
Beispiel #13
0
        public async Task <bool> FileExistsAsync(string name)
        {
            var cachedBytes = await DistributedCache.GetAsync(name);

            if (cachedBytes != null && cachedBytes.Length > 0)
            {
                return(true);
            }
            _client = _client ?? await StorageClient.CreateAsync();

            var obj = await _client.GetObjectAsync(Settings.GoogleStorageBucket, name);

            return(obj?.Size > 0);
        }
        private async Task SetBlobAsync(Blob blob, CancellationToken cancellationToken = default)
        {
            Object item = await _client.GetObjectAsync(_bucketName, NormalisePath(blob.FullPath), cancellationToken : cancellationToken).ConfigureAwait(false);

            if (item.Metadata == null)
            {
                item.Metadata = new Dictionary <string, string>();
            }

            foreach (KeyValuePair <string, string> metadata in blob.Metadata)
            {
                if (item.Metadata.ContainsKey(metadata.Key))
                {
                    item.Metadata[metadata.Key] = metadata.Value;
                }
                else
                {
                    item.Metadata.Add(metadata.Key, metadata.Value);
                }
            }

            await _client.UpdateObjectAsync(item, cancellationToken : cancellationToken).ConfigureAwait(false);
        }
        public async Task <IUnixFileSystemEntry> GetEntryByNameAsync(IUnixDirectoryEntry directoryEntry, string name, CancellationToken cancellationToken)
        {
            IUnixFileSystemEntry result = null;
            var parentEntry             = (GcsDirectoryEntry)directoryEntry;
            var fullName = string.Join("/", new string[] { parentEntry.FullName.Trim('/'), name }).Trim('/');

            Google.Apis.Storage.v1.Data.Object obj = null;
            try
            {
                obj = await _storageClient.GetObjectAsync(_bucketName, fullName, null, cancellationToken);
            } catch { }
            if (obj == null)
            {
                var childEntry = new GcsDirectoryEntry(this)
                {
                    Name = $"{fullName}/"
                };
                var directoryExists = await DirectoryExistsAsync(childEntry);

                if (directoryExists)
                {
                    result = childEntry;
                }
            }
            else
            {
                if (obj.ContentType != "application/x-directory")
                {
                    DateTimeOffset?createdTime = null;
                    if (obj.TimeCreated is DateTime created)
                    {
                        createdTime = new DateTimeOffset(created);
                    }
                    DateTimeOffset?updatedTime = null;
                    if (obj.Updated is DateTime updated)
                    {
                        updatedTime = new DateTimeOffset(updated);
                    }
                    return(new GcsFileEntry(this)
                    {
                        Name = obj.Name,
                        Size = Convert.ToInt64(obj.Size),
                        CreatedTime = createdTime,
                        LastWriteTime = updatedTime
                    });
                }
            }

            return(result);
        }
Beispiel #16
0
        public async Task <bool> ExistsAsync(string path)
        {
            try
            {
                await _googleStorageClient.GetObjectAsync(_bucket, path);

                return(true);
            }
            catch (GoogleApiException ex)
            {
                if (ex.HttpStatusCode == HttpStatusCode.NotFound)
                {
                    return(false);
                }
                throw;
            }
        }
        public async Task <bool> ExistBlob(Uri key)
        {
            string bucket = GetBucketName(key);
            string blob   = GetObjectName(key);

            try
            {
                var obj = await _client.GetObjectAsync(bucket, blob);

                return(bucket != null);
            }
            catch (GoogleApiException e)
                when(e.Error.Code == 404)
                {
                    return(false);
                }
        }
        public async Task <bool> FileExistsAsync(string filename)
        {
            try
            {
                var storageObject = await _storageClient.GetObjectAsync(_configuration.BucketName, filename);

                if (storageObject != null)
                {
                    return(true);
                }
            }
            catch (Exception)
            {
            }

            return(false);
        }
        public async Task <long> GetSizeAsync(string fileName, CancellationToken ct = default)
        {
            Guard.NotNullOrEmpty(fileName, nameof(fileName));

            try
            {
                var obj = await storageClient.GetObjectAsync(bucketName, fileName, null, ct);

                if (!obj.Size.HasValue)
                {
                    throw new AssetNotFoundException(fileName);
                }

                return((long)obj.Size.Value);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                throw new AssetNotFoundException(fileName, ex);
            }
        }
Beispiel #20
0
        public static async Task DeleteFile(
            int projectId,
            string version,
            string filename,
            string branch,
            StorageClient client,
            upwardContext upwardDb,
            accountsContext accountsDb)
        {
            var ver = version.Split(".");

            int major = int.Parse(ver[0]);
            int minor = int.Parse(ver[1]);
            int patch = int.Parse(ver[2]);

            var pkg = await upwardDb.Pkgfile
                      .Where(r => r.Project == projectId && r.Major == major && r.Minor == minor && r.Patch == patch && r.Branch == branch)
                      .FirstOrDefaultAsync();

            var project = await accountsDb.Project
                          .Where(r => r.Id == projectId)
                          .FirstOrDefaultAsync();

            var user = await accountsDb.Userprofile
                       .Where(r => r.Id == project.Userid)
                       .FirstOrDefaultAsync();

            var metaData = await client
                           .GetObjectAsync("upward-test", $"pkg/{projectId}/{branch}/{version}/{filename}");

            pkg.Filename = pkg.Filename.Where(val => val != filename).ToArray();
            pkg.Size    -= (long)metaData.Size;
            user.Size   -= (long)metaData.Size;

            await client.DeleteObjectAsync("upward-test", $"pkg/{projectId}/{branch}/{version}/{filename}");

            await accountsDb.SaveChangesAsync();

            await upwardDb.SaveChangesAsync();
        }
Beispiel #21
0
        public async Task <Fighter> GetFighterByIdAsync(string id)
        {
            var entity = await _db.LookupAsync(_keyFactory.CreateKey(id));

            if (entity == null)
            {
                return(null);
            }

            var fighter = new Fighter
            {
                Id    = id,
                Name  = entity.Properties["name"].StringValue,
                Votes = (int)entity.Properties["vote"].IntegerValue
            };

            var image = await _storage.GetObjectAsync(_bucketName, $"{id}");

            fighter.Image = image.MediaLink;

            return(fighter);
        }
Beispiel #22
0
        public override async Task <FileData> GetAsync(FileGetOptions fileGetOptions)
        {
            GoogleCredential credential = await AuthorizedAsync(fileGetOptions);

            StorageClient storageClient = await StorageClient.CreateAsync(credential);

            FileData file = new FileData();

            switch (fileGetOptions.FileTransfer)
            {
            case FileTransferOptions.Stream:
                await storageClient.DownloadObjectAsync(fileGetOptions.Folder, fileGetOptions.Key, file.Stream);

                break;

            case FileTransferOptions.Url:
                var _file = await storageClient.GetObjectAsync(fileGetOptions.Folder, fileGetOptions.Key);

                if (_file != null)
                {
                    file.Loc = _file.MediaLink;
                }
                break;

            case FileTransferOptions.SecureUrl:
                var urlSigner = UrlSigner.FromServiceAccountCredential((ServiceAccountCredential)credential.UnderlyingCredential);

                if (urlSigner != null)
                {
                    urlSigner.Sign(fileGetOptions.Folder, fileGetOptions.Key, fileGetOptions.SecureLinkTimeToLive);
                }
                break;
            }

            file.Type = "Google Cloud Storage";

            return(file);
        }
Beispiel #23
0
        public async Task <Stream> ReadFileAsync(string name)
        {
            var cachedBytes = await DistributedCache.GetAsync(name);

            if (cachedBytes != null && cachedBytes.Length > 0)
            {
                var ms = new MemoryStream(cachedBytes);
                ms.Position = 0;
                return(ms);
            }

            _client = _client ?? await StorageClient.CreateAsync();

            var obj = await _client.GetObjectAsync(Settings.GoogleStorageBucket, name);

            if (obj?.Size > 0)
            {
                var ms = new MemoryStream();
                await _client.DownloadObjectAsync(Settings.GoogleStorageBucket, name, ms);

                ms.Position = 0;

                if (ms.Length <= Settings.MultimediaMemoryCacheMaxSizeInBytes)
                {
                    // Save to memory cache
                    await DistributedCache.SetAsync(name, ms.ToArray(), new DistributedCacheEntryOptions
                    {
                        SlidingExpiration = Settings.MultimediaMemoryCacheSlidingExpiration
                    });

                    ms.Position = 0;
                }

                return(ms);
            }
            return(null);
        }
Beispiel #24
0
        private static async Task GetObject(StorageClient client, string bucket, string name)
        {
            var obj = await client.GetObjectAsync(bucket, name);

            Console.WriteLine(JsonConvert.SerializeObject(obj, Formatting.Indented));
        }
        //ChangeUserDetails
        public async Task <JsonResult> OnPostUserDetails(Players player, IFormFile picture)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(new JsonResult("Invalid data"));
                }
                Google.Apis.Storage.v1.Data.Object uploadedimage;
                try
                {
                    //Sprawdzanie czy istnieje już zdjęcie profilowe dla tego użytkownika w cloud storage
                    uploadedimage = await _storage.GetObjectAsync(_bucket.BucketName, Player.PlayerId.ToString());

                    //Jeśli nie przesłano zdjęcia profilowego
                    if (picture == null)
                    {
                        _context.Attach(Player).State = EntityState.Modified;
                        //Aktualizacja danych w bazie danych
                        try
                        {
                            Player.Modified       = DateTime.Now;
                            Player.ProfilePicture = uploadedimage.MediaLink;
                            await _context.SaveChangesAsync();

                            return(new JsonResult("Your details has been updated"));
                        }
                        catch (DbUpdateConcurrencyException dbex)
                        {
                            return(new JsonResult("Error while saving to database"));
                        }
                    }

                    //Jeśli przesłano nowe zdjęcie profilowe
                    if (picture.ContentType.Contains("image"))
                    {
                        //Usuwanie aktualnego zdjęcia profilowego z cloud storage
                        await _storage.DeleteObjectAsync(_bucket.BucketName, Player.PlayerId.ToString());

                        //Wysłanie nowego zdjęcie profilowego do cloud storage
                        var pictureStream = picture.OpenReadStream();
                        await _storage.UploadObjectAsync(_bucket.BucketName, Player.PlayerId.ToString(),
                                                         picture.ContentType, pictureStream);

                        //Pobranie danych nowego zdjęcia z cloud storage
                        uploadedimage =
                            await _storage.GetObjectAsync(_bucket.BucketName, Player.PlayerId.ToString());

                        _context.Attach(Player).State = EntityState.Modified;
                        //Aktualizacja danych w bazie danych
                        try
                        {
                            Player.Modified       = DateTime.Now;
                            Player.ProfilePicture = uploadedimage.MediaLink;
                            await _context.SaveChangesAsync();

                            return(new JsonResult("Your details has been updated"));
                        }
                        catch (DbUpdateConcurrencyException dbex)
                        {
                            return(new JsonResult("Error while saving to database"));
                        }
                    }

                    //Podano nie prawidłowy rodzaj pliku
                    return(new JsonResult("Invalid type of file"));
                }
                //Jeśli w nie istnieje zdjęcie profilowe dla tego użytkownika w cloud storage
                catch (Exception ex)
                {
                    //Jeśli nie przesłano zdjęcia profilowego
                    if (picture == null)
                    {
                        _context.Attach(Player).State = EntityState.Modified;
                        //Aktualizacja danych w bazie danych
                        try
                        {
                            Player.Modified = DateTime.Now;
                            await _context.SaveChangesAsync();

                            return(new JsonResult("Your details has been updated"));
                        }
                        catch (DbUpdateConcurrencyException dbex)
                        {
                            return(new JsonResult("Error while saving to database"));
                        }
                    }

                    //Jeśli przesłano nowe zdjęcie profilowe
                    if (picture.ContentType.Contains("image"))
                    {
                        //Wysłanie nowego zdjęcie profilowego do cloud storage
                        var pictureStream = picture.OpenReadStream();
                        await _storage.UploadObjectAsync(_bucket.BucketName, Player.PlayerId.ToString(),
                                                         picture.ContentType, pictureStream);

                        //Pobranie danych nowego zdjęcia z cloud storage
                        uploadedimage =
                            await _storage.GetObjectAsync(_bucket.BucketName, Player.PlayerId.ToString());

                        _context.Attach(Player).State = EntityState.Modified;
                        //Aktualizacja danych w bazie danych
                        try
                        {
                            Player.Modified       = DateTime.Now;
                            Player.ProfilePicture = uploadedimage.MediaLink;
                            await _context.SaveChangesAsync();

                            return(new JsonResult("Your details has been updated"));
                        }
                        catch (DbUpdateConcurrencyException dbex)
                        {
                            return(new JsonResult("Error while saving to database"));
                        }
                    }

                    //Podano nie prawidłowy rodzaj pliku
                    return(new JsonResult("Invalid type of file"));
                }
            }
            catch (Exception ex)
            {
                return(new JsonResult("Request failed"));
            }
        }
Beispiel #26
0
        public async Task <IActionResult> GetBatchImagesAsync()
        {
            JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
            {
                WriteIndented = true
            };
            string requestBody = await new StreamReader(Request.Body).ReadToEndAsync();
            var    data        = JsonConvert.DeserializeObject <BatchImageData[]>(requestBody);

            //  var request = await JsonSerializer.DeserializeAsync<BatchImageData[]>(requestBody, options);
            // var images = request!["images"].EnumerateArray().ToList();

            try
            {
                using var outputStream = new MemoryStream();
                using var archive      = new ZipArchive(outputStream, ZipArchiveMode.Create, true);
                //ms.Position = 0;
                // Will likely be usuful to have TenantData and Property data seperate classes
                foreach (var imageData in data !)
                {
                    using var ms = new MemoryStream();
                    var googleObject = await _storageClient.GetObjectAsync(bucket, imageData.imageUrl);

                    //TODO: Get date taken, this should be done on image caputre
                    await _storageClient.DownloadObjectAsync(googleObject, ms);

                    var imageNameWithExtension = imageData !.imageUrl !.Split("/")[1];
                    var imageExtension         = imageNameWithExtension.Split('.').Last();
                    var rawBestLabel           = imageData !.labels !.OrderByDescending(l => l.Confidence).FirstOrDefault()?.Label;

                    string bestLabel = GetTrimmedLabel(rawBestLabel, 20);

                    //CreateEntry needs a full file name with extension. Can have a directory structure.
                    var zipArchiveEntry = archive.CreateEntry($"{imageData!.tenantName}/{bestLabel}-date/{bestLabel}date.{imageExtension}", CompressionLevel.Optimal);
                    using (var zipStream = zipArchiveEntry.Open())
                    {
                        //await outputStream.CopyToAsync(zipStream);
                        zipStream.Write(ms.ToArray());
                        //zipStream.Flush();
                    }

                    var zipArchiveEntry2 = archive.CreateEntry($"{imageData!.tenantName}/{bestLabel}-date/{bestLabel}-date-labels.txt", CompressionLevel.Optimal);
                    using (var zipStream2 = zipArchiveEntry2.Open())
                    {
                        zipStream2.Write(Encoding.ASCII.GetBytes(string.Concat(imageData !.labels !.Select(l => l.Label + "-" + l.Confidence + "-" + l.Source + "\r").ToList())));
                    }
                }

                archive.Dispose();

                outputStream.Seek(0, SeekOrigin.Begin);

                //var response = File(outputStream.ToArray(), "application/zip", "Images.zip");
                var packageId    = Guid.NewGuid().ToString();
                var uploadResult = await _storageClient.UploadObjectAsync(bucket, "packages/" + packageId + ".zip", MediaTypeNames.Application.Zip, outputStream);

                //return response;
                return(Json(new { id = packageId }));
            }

            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(new BadRequestResult());
            }
        }