gpt4 book ai didi

c#从客户端实时上传到服务器到amazon s3

转载 作者:太空狗 更新时间:2023-10-29 23:51:47 25 4
gpt4 key购买 nike

早上好,我有一个桌面应用程序可以将文件上传到 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(在使用

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

这可能吗?有关于如何操作的指南吗?

提前感谢。

最佳答案

您可以使用 PutObjectRequestInputStream 属性

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);

}
}

关于c#从客户端实时上传到服务器到amazon s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com