Exemplo n.º 1
0
        public override Task <bool> ExistsAsync(FileProviderExistsArgs args)
        {
            var containerName = GetContainerName(args);
            var blobName      = AliyunFileNameCalculator.Calculate(args);
            var ossClient     = GetOssClient(args.Configuration);

            return(Task.FromResult(FileExistsAsync(ossClient, containerName, blobName)));
        }
Exemplo n.º 2
0
        public override Task <bool> DeleteAsync(FileProviderDeleteArgs args)
        {
            var containerName = GetContainerName(args);
            var fileName      = AliyunFileNameCalculator.Calculate(args);
            var ossClient     = GetOssClient(args.Configuration);

            if (!FileExistsAsync(ossClient, containerName, fileName))
            {
                return(Task.FromResult(false));
            }
            ossClient.DeleteObject(containerName, fileName);
            return(Task.FromResult(true));
        }
Exemplo n.º 3
0
        public override async Task <Stream> GetOrNullAsync(FileProviderGetArgs args)
        {
            var containerName = GetContainerName(args);
            var blobName      = AliyunFileNameCalculator.Calculate(args);
            var ossClient     = GetOssClient(args.Configuration);

            if (!FileExistsAsync(ossClient, containerName, blobName))
            {
                return(null);
            }
            var result       = ossClient.GetObject(containerName, blobName);
            var memoryStream = new MemoryStream();
            await result.Content.CopyToAsync(memoryStream);

            return(memoryStream);
        }
Exemplo n.º 4
0
        public override async Task <bool> DownloadAsync(FileProviderDownloadArgs args)
        {
            var containerName = GetContainerName(args);
            var fileName      = AliyunFileNameCalculator.Calculate(args);
            var ossClient     = GetOssClient(args.Configuration);

            if (!FileExistsAsync(ossClient, containerName, fileName))
            {
                return(false);
            }

            var result = ossClient.GetObject(containerName, fileName);

            using (var fs = new FileStream(args.Path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                await result.Content.CopyToAsync(fs);
            }
            return(true);
        }
Exemplo n.º 5
0
        public override Task <string> GetAccessUrlAsync(FileProviderAccessArgs args)
        {
            if (!args.Configuration.HttpAccess)
            {
                return(Task.FromResult(string.Empty));
            }

            var containerName = GetContainerName(args);
            var fileName      = AliyunFileNameCalculator.Calculate(args);
            var ossClient     = GetOssClient(args.Configuration);

            if (!FileExistsAsync(ossClient, containerName, fileName))
            {
                return(Task.FromResult(string.Empty));
            }

            var datetime = args.Expires ?? Clock.Now.AddSeconds(600);
            var uri      = ossClient.GeneratePresignedUri(containerName, fileName, datetime);

            return(Task.FromResult(uri.ToString()));
        }
Exemplo n.º 6
0
        public override Task <string> SaveAsync(FileProviderSaveArgs args)
        {
            var containerName = GetContainerName(args);
            var fileName      = AliyunFileNameCalculator.Calculate(args);
            var aliyunConfig  = args.Configuration.GetAliyunConfiguration();
            var ossClient     = GetOssClient(aliyunConfig);

            if (!args.OverrideExisting && FileExistsAsync(ossClient, containerName, fileName))
            {
                throw new FileAlreadyExistsException($"Saving FILE '{args.FileId}' 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, fileName, args.FileStream);
            return(Task.FromResult(args.FileId));
        }