C#上传从客户机到服务器的实时到Amazon S3 [英] c# upload in real time from client to server to amazon s3
问题描述
早上好,我有一个桌面应用程序,文件上传到WCF服务,然后WCF服务上传到Amazon S3。
Good morning, I have an desktop app that uploads files to a WCF service and then WCF Service uploads to Amazon S3.
这是我的接收文件并上传到S3 WCF方法。
This is my WCF method that receives the file and uploads to 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,而我写的使用内流(
On my client I get the progress in real time when upload the file to the WCF Service but when I get the 100% complete it doesnt mean that the file has already uploaded to S3, so I would like to know if its possible to being uploading the file to S3 while Im writing the stream (inside of the using
(System.IO.MemoryStream writeStream = new System.IO.MemoryStream())
{
这可能吗?如何做到这一点的指引?
Is this possible? Any guideline on how to do it?
鸭preciate提前。
Appreciate in advance.
推荐答案
我会建议将文件上传到WCF的块,而不是流。我这样做,它工作得很好。还需要返回写入到亚马逊以后可以增加基础上的进度条的实际字节的消息。我知道这将导致你写的客户端应用程序,而循环,但它会帮助你,以显示与100%的准确率对于大文件的进展情况。你的WCF功能应该采取这样的参数
I would recommend uploading the file to the WCF as chunks instead of a stream. I did so and it works just fine. also you need to return a message of the actual bytes written to the amazon later on you can increase the progress bar based on that. I know it will cause you to write a while loop in the client application but it will help you to show the progress with 100% accuracy for large files. Your WCF function should take the parameter like these
[DataContract]
class RemoteFileInfo
{
[DataMember]
Byte[] myChunk;
[DataMember]
long myOffset;
// other stuff you think you need to be sent each time.
}
这篇关于C#上传从客户机到服务器的实时到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!