public async Task <Uri> Add(Stream stream, T info)
        {
            InitStorage();
            var targetFile = nameProvider.GetName(info);

            Trace.WriteLine($"Writing {targetFile} azure");
            var blockBlob = cloudBlobContainer.GetBlockBlobReference(targetFile);

            blockBlob.Properties.ContentType  = info.MimeType;
            blockBlob.Properties.CacheControl = cacheControl;

            if (!info.MimeType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
            {
                blockBlob.Properties.ContentDisposition =
                    $"attachment;filename=UTF-8''{Uri.EscapeDataString(info.OriginalName)}";
            }

            if (info.Extra != null)
            {
                foreach (var kvp in info.Extra)
                {
                    blockBlob.Metadata.Add(kvp);
                }
            }

            await blockBlob.UploadFromStreamAsync(stream);

            return(blockBlob.Uri);
        }
Esempio n. 2
0
        public async Task <Uri> Add(Stream stream, T info)
        {
            var key = _nameProvider.GetName(info);

            using (var client = new AmazonS3Client(_accessKey, _secretKey, RegionEndpoint.EUCentral1))
            {
                var putRequest = new PutObjectRequest
                {
                    BucketName      = _bucketName,
                    Key             = key,
                    InputStream     = stream,
                    AutoCloseStream = true,
                    CannedACL       = S3CannedACL.PublicRead
                };
                putRequest.Metadata.Add(nameof(info.OriginalName), info.OriginalName);
                putRequest.Metadata.Add(nameof(info.MimeType), info.MimeType);
                putRequest.Metadata.Add(nameof(info.Owner), info.Owner);

                await client.PutObjectAsync(putRequest);

                return(new Uri(string.Format(_awsUrlFormat, RegionEndpoint.EUCentral1.SystemName, _bucketName, key)));
            }
        }
Esempio n. 3
0
        public async Task <Uri> Add(Stream stream, T info)
        {
            var filename     = nameProvider.GetName(info);
            var fullFilePath = Path.Combine(baseFolder, filename);
            var triesCount   = 0;

            for (; File.Exists(fullFilePath) && triesCount < MaxTriesCount; triesCount++)
            {
                filename     = nameProvider.Regenerate(info, filename);
                fullFilePath = Path.Combine(baseFolder, filename);
            }

            if (triesCount > MaxTriesCount)
            {
                throw new IOException("Could not generate unique file name");
            }

            using (var dest = new FileStream(fullFilePath, FileMode.CreateNew))
            {
                await stream.CopyToAsync(dest);
            }

            return(new Uri(baseUri, filename));
        }