Пример #1
0
        public override async Task SaveAsync(BlobProviderSaveArgs args)
        {
            var container = await GetOrCreateContainerAsync(args.ContainerName, args.CancellationToken);

            var blob = await DatabaseBlobRepository.FindAsync(
                container.Id,
                args.BlobName,
                args.CancellationToken
                );

            var content = await args.BlobStream.GetAllBytesAsync(args.CancellationToken);

            if (blob != null)
            {
                if (!args.OverrideExisting)
                {
                    throw new BlobAlreadyExistsException(
                              $"Saving BLOB '{args.BlobName}' does already exists in the container '{args.ContainerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
                }

                blob.SetContent(content);

                await DatabaseBlobRepository.UpdateAsync(blob, autoSave : true);
            }
            else
            {
                blob = new DatabaseBlob(GuidGenerator.Create(), container.Id, args.BlobName, content, CurrentTenant.Id);
                await DatabaseBlobRepository.InsertAsync(blob, autoSave : true);
            }
        }
Пример #2
0
        public override async Task SaveAsync(BlobProviderSaveArgs args)
        {
            var ossClient = await GetOssClientAsync(args);

            var blobName      = AliyunBlobNameCalculator.Calculate(args);
            var configuration = args.Configuration.GetAliyunConfiguration();

            // 先检查Bucket
            if (configuration.CreateBucketIfNotExists)
            {
                await CreateBucketIfNotExists(ossClient, args, configuration.CreateBucketReferer);
            }

            var bucketName = GetBucketName(args);

            // 是否已存在
            if (await BlobExistsAsync(ossClient, args, blobName))
            {
                // 是否覆盖
                if (!args.OverrideExisting)
                {
                    throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the bucketName '{GetBucketName(args)}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
                }
                else
                {
                    // 删除原文件
                    ossClient.DeleteObject(bucketName, blobName);
                }
            }
            // 保存
            ossClient.PutObject(bucketName, blobName, args.BlobStream);
        }
Пример #3
0
        public override async Task SaveAsync(BlobProviderSaveArgs args)
        {
            var blobName      = AwsBlobNameCalculator.Calculate(args);
            var configuration = args.Configuration.GetAwsConfiguration();
            var containerName = GetContainerName(args);

            using (var amazonS3Client = await GetAmazonS3Client(args))
            {
                if (!args.OverrideExisting && await BlobExistsAsync(amazonS3Client, containerName, blobName))
                {
                    throw new BlobAlreadyExistsException(
                              $"Saving BLOB '{args.BlobName}' does already exists in the container '{containerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
                }

                if (configuration.CreateContainerIfNotExists)
                {
                    await CreateContainerIfNotExists(amazonS3Client, containerName);
                }

                await amazonS3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName  = containerName,
                    Key         = blobName,
                    InputStream = args.BlobStream
                });
            }
        }
Пример #4
0
    public override async Task SaveAsync(BlobProviderSaveArgs args)
    {
        var filePath = FilePathCalculator.Calculate(args);

        if (!args.OverrideExisting && await ExistsAsync(filePath))
        {
            throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{args.ContainerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
        }

        DirectoryHelper.CreateIfNotExists(Path.GetDirectoryName(filePath));

        var fileMode = args.OverrideExisting
            ? FileMode.Create
            : FileMode.CreateNew;

        await Policy.Handle <IOException>()
        .WaitAndRetryAsync(2, retryCount => TimeSpan.FromSeconds(retryCount))
        .ExecuteAsync(async() =>
        {
            using (var fileStream = File.Open(filePath, fileMode, FileAccess.Write))
            {
                await args.BlobStream.CopyToAsync(
                    fileStream,
                    args.CancellationToken
                    );

                await fileStream.FlushAsync();
            }
        });
    }
Пример #5
0
        public override Task SaveAsync(BlobProviderSaveArgs args)
        {
            //TODO...
            var containerName = args.ContainerName;
            var blobName      = $"{args.BlobName}";

            var configurationSection = _configuration.GetSection("Qiniu");

            Mac       mac       = new Mac(configurationSection["AccessKey"], configurationSection["SecretKey"]);// AK SK使用
            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = configurationSection["Bucket"];
            string token  = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());//token生成
            Config config = new Config()
            {
                Zone     = Zone.ZONE_CN_South,
                UseHttps = true
            };
            FormUploader upload = new FormUploader(config);
            HttpResult   result = new HttpResult();

            Stream stream = args.BlobStream;

            result = upload.UploadStream(stream, blobName, token, null);
            if (result.Code != 200)
            {
                throw new Exception(result.RefText);//上传失败错误信息
            }

            return(Task.CompletedTask);
        }
        public async Task Save_Test()
        {
            var containerName = "b2b";
            var blobName      = "appsettings.json";
            var tenantId      = Guid.NewGuid();

            var file = new FileStream("appsettings.json", FileMode.Open);

            using (_currentTenant.Change(tenantId, "test"))
            {
                var blod = _calculator.Calculate(GetArgs(containerName, blobName));
                var tt   = new BlobProviderSaveArgs(
                    containerName,
                    new BlobContainerConfiguration().UseTencentCloudCos(cos =>
                {
                    //cos.AppId = "";
                    //cos.SecretId = "";
                    //cos.SecretKey = "";
                    //cos.ContainerName = "";
                    cos.Region = EnumUtils.GetValue(CosRegion.AP_Guangzhou);
                }),
                    blobName,
                    file, true
                    );
                await _blob.SaveAsync(tt);
            }
        }
Пример #7
0
        public override async Task SaveAsync(BlobProviderSaveArgs args)
        {
            var blobName      = AzureBlobNameCalculator.Calculate(args);
            var configuration = args.Configuration.GetAzureConfiguration();

            if (!args.OverrideExisting && await BlobExistsAsync(args, blobName))
            {
                throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{GetContainerName(args)}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
            }

            if (configuration.CreateContainerIfNotExists)
            {
                await CreateContainerIfNotExists(args);
            }

            await GetBlobClient(args, blobName).UploadAsync(args.BlobStream, true);
        }
        /// <summary>
        /// 七牛文件上传保存
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public override Task SaveAsync(BlobProviderSaveArgs args)
        {
            //TODO...
            //使用 GetMyCustomBlobProviderConfiguration 方法访问额外的选项:
            //var config = args.Configuration.GetMyCustomBlobProviderConfiguration();
            //var value = config.MyOption1;


            var containerName = args.ContainerName;
            var blobName      = $"{args.BlobName}";


            var configurationSection = _configuration.GetSection("Qiniu");

            Mac       mac       = new Mac(configurationSection["AccessKey"], configurationSection["SecretKey"]);// AK SK使用
            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = configurationSection["Bucket"];
            string token  = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());//token生成
            Config config = new Config()
            {
                Zone     = Zone.ZONE_CN_South,
                UseHttps = true
            };

            FormUploader  upload = new FormUploader(config);
            HttpResult    result = new HttpResult();
            List <Object> list   = new List <Object>();


            //var _qiniuName = "qiniu" + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff");//重命名文件加上时间戳
            Stream stream = args.BlobStream;

            result = upload.UploadStream(stream, blobName, token, null);
            if (result.Code == 200)
            {
                list.Add(new { fileName = "", qiniuName = blobName, uploadTime = DateTime.Now, Remark = "" });
            }
            else
            {
                throw new Exception(result.RefText);//上传失败错误信息
            }

            return(Task.CompletedTask);
        }
Пример #9
0
    public override async Task SaveAsync(BlobProviderSaveArgs args)
    {
        var blobName      = MinioBlobNameCalculator.Calculate(args);
        var configuration = args.Configuration.GetMinioConfiguration();
        var client        = GetMinioClient(args);
        var containerName = GetContainerName(args);

        if (!args.OverrideExisting && await BlobExistsAsync(client, containerName, blobName))
        {
            throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{containerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
        }

        if (configuration.CreateBucketIfNotExists)
        {
            await CreateBucketIfNotExists(client, containerName);
        }

        await client.PutObjectAsync(containerName, blobName, args.BlobStream, args.BlobStream.Length);
    }
Пример #10
0
        public override Task SaveAsync(BlobProviderSaveArgs args)
        {
            var containerName = GetContainerName(args);
            var blobName      = AliyunBlobNameCalculator.Calculate(args);
            var aliyunConfig  = args.Configuration.GetAliyunConfiguration();
            var ossClient     = GetOssClient(aliyunConfig);

            if (!args.OverrideExisting && BlobExists(ossClient, containerName, blobName))
            {
                throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{containerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
            }
            if (aliyunConfig.CreateContainerIfNotExists)
            {
                if (!ossClient.DoesBucketExist(containerName))
                {
                    ossClient.CreateBucket(containerName);
                }
            }
            ossClient.PutObject(containerName, blobName, args.BlobStream);
            return(Task.CompletedTask);
        }
        /// <summary>
        /// 保存内容
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public override async Task SaveAsync(BlobProviderSaveArgs args)
        {
            var blobName = BlobNameCalculator.Calculate(args);

            if (!args.OverrideExisting && await BlobExistsAsync(args, blobName))
            {
                throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{GetContainerName(args)}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
            }

            await CreateContainerIfNotExists(args);

            byte[] data = new byte[args.BlobStream.Length];
            await args.BlobStream.ReadAsync(data, 0, data.Length);

            var request = new PutObjectRequest(GetBucket(args), blobName, data);

            //设置签名有效时长
            request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

            var result = GetClient(args).PutObject(request);
        }
Пример #12
0
 public virtual Task SaveAsync(BlobProviderSaveArgs args)
 {
     throw new System.NotImplementedException();
 }
Пример #13
0
 public abstract Task SaveAsync(BlobProviderSaveArgs args);