我们可以使用C#直接将二进制文件写入S3吗?

时间:2017-11-20 05:56:25

标签: c# amazon-s3 binary ceph

我们可以使用C#直接将字节写入S3 [File.dat]。我要求在S3存储系统中连续将二进制数据写入文件。我能够将文件从本地系统复制到S3。但是我无法直接写入S3 任何人都可以帮我解决这个问题吗?有人说通过使用多部分上传。但是他们根据长度传输大文件。

  private const string AWS_ACCESS_KEY = "Access Key";
  private const string AWS_SECRET_KEY = "Security Key";

  private void Form1_Load(object sender, EventArgs e)
    {
        AmazonS3Config s3config = new AmazonS3Config();
        s3config.ServiceURL = "https://console.aws.amazon.com/console/";

        AmazonS3Client client = new AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, s3config);
        CreateFilesInBucket(client);
        string filepath = @"C:\Users\Documents\Test Folder\Test2.csv";
        UploadChunk(client,filepath); //Edited
    }

  private static void CreateFilesInBucket(AmazonS3Client client)
    {            
        Byte[] DatFile = File.ReadAllBytes(@"C:\Users\Documents\Test Folder\File.DAT");
        PutObjectRequest request = new PutObjectRequest();          
        request.BucketName = "TestBucket";           
        request.ContentType = "binary";
        request.FilePath = @"C:\Users\Documents\Test Folder\File.DAT";
        client.PutObject(request);            
    }

编辑: 我正在尝试使用下面的代码,但它会抛出错误。

 public void UploadChunk(AmazonS3Client client, string filepath)
    {
        List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();
        List<PartETag> lsttags = new List<PartETag>();
        // 1. Initialize.
        InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
        {
            BucketName = "TestBucket",
            Key = "Test4.dat"
        };

        InitiateMultipartUploadResponse initResponse =
            client.InitiateMultipartUpload(initiateRequest);

        // 2. Upload Parts.
        long contentLength = new FileInfo(filepath).Length;
        long partSize = 5242880; // 5 MB
        long sizechk = 5242880;
        try
        {
            MemoryStream chnkmemry;
            byte[] bteary = File.ReadAllBytes(filepath);

            long filePosition = 0;
            long length = 0;
            for (int i = 1; filePosition < contentLength; i++)
            {
                chnkmemry = new MemoryStream();
                partSize = Math.Min(partSize, (contentLength - filePosition));
                  if (partSize < sizechk ) break;
                chnkmemry.Write(bteary, (int)length, (int)partSize);


                length += chnkmemry.Length;
                // Create request to upload a part.
                UploadPartRequest uploadRequest = new UploadPartRequest
                {
                    BucketName = "TestBucket",
                    Key = "Test4.dat",
                    UploadId = initResponse.UploadId,
                    PartNumber = i,
                    PartSize = partSize,
                    FilePosition = filePosition,
                    InputStream = chnkmemry
                };

                // Upload part and add response to our list.
                uploadResponses.Add(client.UploadPart(uploadRequest));
                lsttags.Add(new PartETag(i, uploadResponses[i-1].ETag));  
                filePosition += partSize;
            }

            // Step 3: complete.
            CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
            {
                BucketName = "TestBucket",
                Key = "Test4.dat",
                UploadId = initResponse.UploadId,
                PartETags = lsttags
            };

            CompleteMultipartUploadResponse completeUploadResponse =
              client.CompleteMultipartUpload(completeRequest);

        }
        catch (Exception exception)
        {
            MessageBox.Show("Exception occurred: "+ exception.Message);
            AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest
            {
                BucketName = "TestBucket",
                Key = "Test4.dat",
                UploadId = initResponse.UploadId
            };
            client.AbortMultipartUpload(abortMPURequest);
        }
    }

使用错误代码EntityTooSmall和Http状态代码BadRequest发出请求时出错。服务未返回任何其他错误信息。

提前致谢。

0 个答案:

没有答案