public ICloudFileMetadata GetFileMetadata(string fileName, string bucketName = "") { EnsureBucketExist(bucketName); try { string filePath = Path.Combine(RootPath , fileName); var fileInfo = new FileInfo(filePath); var output = new CloudFileMetadata { Bucket = _bucketName, Size = (ulong)fileInfo.Length, Name = GetFileName(fileInfo.FullName), LastModified = fileInfo.LastWriteTimeUtc, ContentType = MimeTypesMap.GetMimeType(fileInfo.Name), SignedUrl = fileInfo.FullName }; return(output); } catch (FileNotFoundException fnfe) { _logger.LogError(fnfe, $"{nameof(GetFileMetadata)} : File Not Found - {{fileName}} in bucket {{bucketName}}", fileName, _bucketName); 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 destinationPath = Path.Combine(_options.Path , _bucketName , targetPath); //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); try { if (!Directory.Exists(RootPath)) { throw new DirectoryNotFoundException($"{RootPath} does not exists."); } Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)); using (FileStream fileStream = File.Create(destinationPath)) { source.CopyTo(fileStream); } } catch (Exception e) { _logger.LogError(e, e.Message); throw; } var fileInfo = new FileInfo(destinationPath); var output = new CloudFileMetadata { Bucket = _bucketName , Size = (ulong)fileInfo.Length , Name = GetFileName(fileInfo.FullName) , LastModified = fileInfo.LastWriteTimeUtc , ContentType = MimeTypesMap.GetMimeType(fileInfo.Name) }; return(output); }