5

早上好,我有一个桌面应用程序将文件上传到 WCF 服务,然后 WCF 服务上传到 Amazon S3。

这是我接收文件并上传到 S3 的 WCF 方法。

public void UploadFile(RemoteFileInfo request)
        {
            config = new AmazonS3Config();
            config.CommunicationProtocol = Protocol.HTTP;
            accessKeyID = "XXXXXXX"; 
            secretAccessKeyID = "YYYYYYYY";
            client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config);

            int chunkSize = 2048;
            byte[] buffer = new byte[chunkSize];

            using (System.IO.MemoryStream writeStream = new System.IO.MemoryStream())
            {
                do
                {
                    // read bytes from input stream
                    int bytesRead = request.FileByteStream.Read(buffer, 0, chunkSize);
                    if (bytesRead == 0) break;

                    // simulates slow connection
                    System.Threading.Thread.Sleep(3);

                    // write bytes to output stream
                    writeStream.Write(buffer, 0, bytesRead);
                } while (true);

                // report end
                Console.WriteLine("Done!");

                // start the uploading to S3
                PutObjectRequest fileRequest = new PutObjectRequest();
                fileRequest.WithInputStream(writeStream);
                fileRequest.Key = "testfile.pdf";
                fileRequest.WithBucketName("tempbucket");
                fileRequest.CannedACL = S3CannedACL.Private;
                fileRequest.StorageClass = S3StorageClass.Standard;
                client.PutObject(fileRequest);

                writeStream.Close();
            }

        }

在我的客户端上,当我将文件上传到 WCF 服务时,我会实时获得进度,但是当我完成100% 时,这并不意味着文件已经上传到 S3,所以我想知道它是否可以上传在我写入流时将文件写入 S3(在 using 内部

(System.IO.MemoryStream writeStream = new System.IO.MemoryStream())
            {

这可能吗?关于如何做的任何指导?

提前欣赏。

4

2 回答 2

0

您可以使用PutObjectRequest的InputStream属性

public void UploadFile(RemoteFileInfo request)
{
    config = new AmazonS3Config();
    config.CommunicationProtocol = Protocol.HTTP;
    accessKeyID = "XXXXXXX"; 
    secretAccessKeyID = "YYYYYYYY";
    client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID,secretAccessKeyID,config);

    int chunkSize = 2048;
    byte[] buffer = new byte[chunkSize];

    PutObjectRequest fileRequest = new PutObjectRequest();

    fileRequest.Key = "testfile.pdf";
    fileRequest.WithBucketName("tempbucket");
    fileRequest.CannedACL = S3CannedACL.Private;
    fileRequest.StorageClass = S3StorageClass.Standard;

    using (fileRequest.InputStream = new System.IO.MemoryStream())
    {
      do
      {
         // read bytes from input stream
         int bytesRead = request.FileByteStream.Read(buffer, 0, chunkSize);
         if (bytesRead == 0) break;

         // simulates slow connection
         System.Threading.Thread.Sleep(3);

         // write bytes to output stream
         fileRequest.InputStream.Write(buffer, 0, bytesRead);
       } while (true);

       // report end
       Console.WriteLine("Done!");

       client.PutObject(fileRequest);

    }
 }
于 2013-02-27T14:18:37.803 回答
0

我建议将文件作为块而不是流上传到 WCF。我这样做了,它工作得很好。您还需要稍后返回写入亚马逊的实际字节的消息,您可以在此基础上增加进度条。我知道这会导致您在客户端应用程序中编写一个 while 循环,但它会帮助您以 100% 的准确性显示大文件的进度。您的 WCF 函数应采用如下参数

[DataContract]
class RemoteFileInfo
{
    [DataMember]
    Byte[] myChunk;
    [DataMember]
    long myOffset;
    // other stuff you think you need to be sent each time.

}
于 2013-02-27T14:28:13.083 回答