c#实时从客户端上传到服务器到亚马逊s3

时间:2013-02-27 13:36:07

标签: c# wcf amazon-s3

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

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

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(在使用

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

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

提前感谢。

2 个答案:

答案 0 :(得分: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);

    }
 }

答案 1 :(得分: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.

}