使用ASP.NET SDK将文件上载到S3存储桶的文件夹

时间:2011-08-12 12:00:14

标签: asp.net sdk amazon-s3

如何使用AWS SDK for ASP.NET将文件上传到特定文件夹? - 我能够通过指定存储桶名称(request.WithBucketName)来上传文件,但我希望能够将文件上传到存储桶本身的特定文件夹。

这是我用来将文件上传到单个存储桶的代码:

public bool UploadFileToS3(string uploadAsFileName, Stream ImageStream, S3CannedACL filePermission, S3StorageClass storageType, string toWhichBucketName)
{

    try
    {
        client = Amazon.AWSClientFactory.CreateAmazonS3Client(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY);
        PutObjectRequest request = new PutObjectRequest();
        request.WithKey(uploadAsFileName);
        request.WithInputStream(ImageStream);
        request.WithBucketName(toWhichBucketName);
        request.CannedACL = filePermission;
        request.StorageClass = storageType;

        client.PutObject(request);
        client.Dispose();
    }
    catch
    {            
        return false;
    }
    return true;

}

希望此代码可以帮助您。

3 个答案:

答案 0 :(得分:18)

要将文件添加到存储桶中的文件夹,您需要更新PutObjectRequest的Key以在文件名前包含该文件夹。

public bool UploadFileToS3(string uploadAsFileName, Stream ImageStream, S3CannedACL filePermission, S3StorageClass storageType, string toWhichBucketName)
{
    try
    {
        using(client = Amazon.AWSClientFactory.CreateAmazonS3Client(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY))
        {
           PutObjectRequest request = new PutObjectRequest();
           request.WithKey( "folder" + "/" + uploadAsFileName );
           request.WithInputStream(ImageStream);
           request.WithBucketName(toWhichBucketName);
           request.CannedACL = filePermission;
           request.StorageClass = storageType;

           client.PutObject(request);
        }
    }
    catch
    {            
        return false;
    }
    return true;
}

This post介绍如何将文件上传到文件夹。他们正在使用TransferUtilityUploadRequest,但它应该与PutObjectRequest一起使用。滚动到相关示例的底部。

This post显示了如何在不向其上传文件的情况下创建文件夹。

希望这是有帮助的

编辑: 更新了代码以使用using块,而不是按照最佳实践调用Dispose。

答案 1 :(得分:2)

看起来像关注functionlity

1.创建AmazonS3对象

2.创建一个桶

3.将新文件添加到Amazon S3

4.从Amazon S3获取文件

5.删除Amazon S3中的文件

Amazon

答案 2 :(得分:2)

超级简单的方法:

using System;
using System.Web;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System.Configuration;

/// <summary>
/// Summary description for AWShandler
/// </summary>
public static class AWSHandler
{
    public static void sendFileToS3(string fileName, string storeLocation)
    {
        try
        {
            AmazonS3Client client = new AmazonS3Client(RegionEndpoint.EUWest1);
            PutObjectRequest request = new PutObjectRequest();
            request.BucketName = ConfigurationManager.AppSettings["AWSBucket"].ToString();
            request.FilePath = fileName;
            request.Key = storeLocation + fileName;
            request.ContentType = MimeMapping.GetMimeMapping(fileName);
            PutObjectResponse response = client.PutObject(request);
        }
        catch (Exception ex)
        {
            // use a logger and handle it
        }
    }
}

您只需将密钥放在web / app.config文件中:

<add key="AWSAccessKey" Value="yourKey" />
<add key="AWSSecretKey" Value="yourSecret" />

可以从AWS控制台中的您的帐户页面获取这些内容。他们也必须使用此处引用的名称,因为它们是由AWS库预先定义的。