コード例 #1
0
        /// <summary>单文件上传
        /// </summary>
        private async Task <FileIdentifier> SingleUploadFile(S3Patch patch, UploadFileInfo info)
        {
            var fileId = _fileIdGenerator.GenerateFileId(StoreType.S3, patch, info);

            var putObjectRequest = new PutObjectRequest()
            {
                BucketName      = patch.Bucket.Name,
                AutoCloseStream = true,
                Key             = fileId,
                CannedACL       = GetS3CannedACL((S3Acl)patch.Bucket.Acl)
            };

            if (info.FileData != null)
            {
                putObjectRequest.InputStream = new MemoryStream(info.FileData);
            }
            else
            {
                putObjectRequest.FilePath = info.FilePath;
            }

            var client = _s3ClientFactory.GetClientByBucket(patch.Bucket);

            var putObjectResponse = await client.PutObjectAsync(putObjectRequest);

            var fileIdentifier = new FileIdentifier()
            {
                FileId    = fileId,
                Patch     = patch,
                StoreType = StoreType.S3
            };

            return(fileIdentifier);
        }
コード例 #2
0
        /// <summary>分片上传数据
        /// </summary>
        private async Task <FileIdentifier> MultipartUploadFile(S3Patch patch, UploadFileInfo info)
        {
            var fileId = _fileIdGenerator.GenerateFileId(StoreType.S3, patch, info);

            //获取S3客户端
            var client = _s3ClientFactory.GetClientByBucket(patch.Bucket);

            //初始化分片上传

            var initiateMultipartUploadResponse = await client.InitiateMultipartUploadAsync(patch.Bucket.Name, fileId);

            //上传Id
            var uploadId = initiateMultipartUploadResponse.UploadId;
            // 计算分片总数。
            var partSize = patch.Bucket.SliceSize;
            //var fi = new FileInfo(spoolFile.FilePath);//?
            var fileSize  = GetFileSize(info);
            var partCount = fileSize / partSize;

            if (fileSize % partSize != 0)
            {
                partCount++;
            }

            // 开始分片上传。partETags是保存partETag的列表,OSS收到用户提交的分片列表后,会逐一验证每个分片数据的有效性。 当所有的数据分片通过验证后,OSS会将这些分片组合成一个完整的文件。
            var partETags = new List <PartETag>();

            using (var fs = File.Open(info.FilePath, FileMode.Open))
            {
                for (var i = 0; i < partCount; i++)
                {
                    var skipBytes = (long)partSize * i;
                    // 计算本次上传的片大小,最后一片为剩余的数据大小。
                    var size = (int)((partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes));

                    byte[] buffer = new byte[size];
                    fs.Read(buffer, 0, size);

                    //分片上传
                    var uploadPartResponse = await client.UploadPartAsync(new UploadPartRequest()
                    {
                        BucketName  = patch.Bucket.Name,
                        UploadId    = uploadId,
                        Key         = fileId,
                        InputStream = new MemoryStream(buffer),
                        PartSize    = size,
                        PartNumber  = i + 1
                    });

                    partETags.Add(new PartETag(uploadPartResponse.PartNumber, uploadPartResponse.ETag));
                    _logger.LogDebug("上传文件Key:{0},UploadId:{1},完成 {2}/{3}", fileId, uploadId, partETags.Count, partCount);
                }
            }

            //完成上传分片
            var completeMultipartUploadResponse = await client.CompleteMultipartUploadAsync(new CompleteMultipartUploadRequest()
            {
                BucketName = patch.Bucket.Name,
                Key        = fileId,
                UploadId   = uploadId,
                PartETags  = partETags
            });

            _logger.LogDebug("分片上传完成,Key:{0},Tags:[{1}]", completeMultipartUploadResponse.Key, string.Join(",", partETags));


            var fileIdentifier = new FileIdentifier()
            {
                FileId    = fileId,
                Patch     = patch,
                StoreType = StoreType.S3
            };

            return(fileIdentifier);
        }