public ICloudFileMetadata GetFileMetadata(string fileName, string bucketName = "") { EnsureBucketExist(bucketName); try { Object file = _storageClient.GetObject(_bucketName, fileName); var output = new CloudFileMetadata { Bucket = file.Bucket, Size = file.Size, Name = file.Name, LastModified = file.Updated, ContentType = file.ContentType, SignedUrl = _urlSigner.Sign(file.Bucket, file.Name, new TimeSpan(1, 0, 0), HttpMethod.Get) }; return(output); } catch (GoogleApiException gex) { if (gex.HttpStatusCode == HttpStatusCode.NotFound) { _logger.LogError("Not Found - {fileName} in bucket {bucketName}", fileName, _bucketName); //! References looks for starting part of the following message to identify file not found error throw new ArgumentException($"Not Found - {fileName} in bucket {_bucketName}", gex); } _logger.LogError(-1, gex, gex.Error.Message); throw; } }
public ICloudFileMetadata UploadStream(Stream source, string targetPath, string contentType = "application/octet-stream", string bucketName = "") { EnsureBucketExist(bucketName); //bool fileNameExistInTargetFolder = Path.GetExtension(targetPath) != string.Empty; //if (!fileNameExistInTargetFolder) //{ // throw new ArgumentException($"'{nameof(targetPath)}' does not have a valid file extension"); //} string fileName = Path.GetFileName(targetPath); string directoryName = Path.GetDirectoryName(targetPath); string correctedDirectoryName = directoryName.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) .Trim(Path.AltDirectorySeparatorChar); string destinationFileName = (correctedDirectoryName + Path.AltDirectorySeparatorChar + fileName).TrimStart(Path.AltDirectorySeparatorChar); var progress = new Progress <IUploadProgress>(p => _logger.LogTrace($"destination gs://{{bucket}}/{{destinationFileName}}, bytes: {p.BytesSent}, status: {p.Status}", _bucketName, destinationFileName)); source.Seek(0, SeekOrigin.Begin); Object upload; try { upload = _storageClient.UploadObject(_bucketName, destinationFileName, contentType, source, progress: progress); } catch (GoogleApiException gex) { _logger.LogError(-1, gex, gex.Error.Message); throw; } var output = new CloudFileMetadata { Bucket = _bucketName, Size = upload.Size, LastModified = upload.Updated, ContentType = upload.ContentType, Name = destinationFileName }; return(output); }