【问题标题】:Uploading a HttpPostedFile to S3将 HttpPostedFile 上传到 S3
【发布时间】:2010-12-25 02:50:11
【问题描述】:

我正在帮助当地一所学校完成一些涉及 .Net 和 S3 的项目。 其中我对两者都是新手。我们想通过 Flash 上传表单上传文件,然后通过 .Net 将其存储在 Amazon S3 上。我已经下载并浏览了这些示例,但此时我陷入了困境。

 public string postFile(HttpPostedFile file)
   {
       var recFile = file;
       try
       {
       var client = AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID);
           var request = new PutObjectRequest();
           request.WithMetaData("title", "the title")
                    .WithContentBody(???)
                    .WithBucketName(bucketName)
                    .WithKey(keyName);

           using (S3Response response = client.PutObject(request))
           {
               return keyName;
           }
       }
       ......

我认为这是要走的路,但不知道如何真正让它发挥作用。 如果是这样的话,我会坚持下去,但希望任何有过这方面经验的人提个醒。谢谢。

【问题讨论】:

  • 上传文件好运吗?我根本没有上传我的文件:(

标签: c# .net amazon-s3


【解决方案1】:

这行得通吗?

public string postFile(HttpPostedFile file)
{
    var recFile = file;
    try
    {
        var client = AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID);
        var request = new PutObjectRequest();

        request.WithMetaData("title", "the title")
        request.WithInputStream(recFile.InputStream); //** Using InputStream
        request.WithBucketName(bucketName);
        request.WithKey(keyName);

        using (S3Response response = client.PutObject(request))
        {
            return keyName;
        }
    ......

【讨论】:

  • 那行得通,这种方法的问题是文件将被 ASP.NET 缓冲到磁盘,然后在另一个请求中发送到 S3,这将使有效传输时间加倍。不幸的是,编写一个模块来处理原始流需要相当多的工作,因此位是即时传输的。
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多