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

时间:2022-09-07 10:45:35

How do I use AWS SDK for ASP.NET to upload a file to a specific folder? - I was able to upload files by specifying the bucket name (request.WithBucketName), but I want to be able to upload a file to a specific folder within the bucket itself.

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

This is the code that I use to upload a file to a single bucket:

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

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;

}

Hope that this code will help you out.

希望这段代码能帮到你。

3 个解决方案

#1


18  

To add a file to a folder in a bucket, you need to update the Key of the PutObjectRequest to include the folder before the file name.

要将文件添加到存储桶中的文件夹,您需要更新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 that talks about uploading files to folder. They are using a TransferUtilityUploadRequest though, but it should work with the PutObjectRequest. Scroll to the bottom for the relevant example.

这篇文章讨论了如何将文件上传到文件夹。他们正在使用TransferUtilityUploadRequest,但它应该与PutObjectRequest一起使用。滚动到相关示例的底部。

This post shows how to create a folder without uploading a file to it.

这篇文章展示了如何在不向其上传文件的情况下创建文件夹。

Hope this is helpful

希望这有用

Edit: Updated the code to use a using block instead of calling Dispose per best practices.

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

#2


2  

Look Like Following functionlity

看起来像关注功能

1.Create an AmazonS3 object

1.创建一个AmazonS3对象

2.Create a bucket

2.创建一个桶

3.Add a new file to Amazon S3

3.将新文件添加到Amazon S3

4.Get a file from Amazon S3

4.从Amazon S3获取文件

5.Delete a file from Amazon S3

5.从Amazon S3删除文件

Amazon

#3


2  

super easy way:

超级简单的方法:

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
        }
    }
}

you just need to put your keys in the web/app.config file:

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

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

These can be obtained from you account page in the AWS console. They must use the names quoted here too, as they are pre-defined by the AWS library.

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

#1


18  

To add a file to a folder in a bucket, you need to update the Key of the PutObjectRequest to include the folder before the file name.

要将文件添加到存储桶中的文件夹,您需要更新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 that talks about uploading files to folder. They are using a TransferUtilityUploadRequest though, but it should work with the PutObjectRequest. Scroll to the bottom for the relevant example.

这篇文章讨论了如何将文件上传到文件夹。他们正在使用TransferUtilityUploadRequest,但它应该与PutObjectRequest一起使用。滚动到相关示例的底部。

This post shows how to create a folder without uploading a file to it.

这篇文章展示了如何在不向其上传文件的情况下创建文件夹。

Hope this is helpful

希望这有用

Edit: Updated the code to use a using block instead of calling Dispose per best practices.

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

#2


2  

Look Like Following functionlity

看起来像关注功能

1.Create an AmazonS3 object

1.创建一个AmazonS3对象

2.Create a bucket

2.创建一个桶

3.Add a new file to Amazon S3

3.将新文件添加到Amazon S3

4.Get a file from Amazon S3

4.从Amazon S3获取文件

5.Delete a file from Amazon S3

5.从Amazon S3删除文件

Amazon

#3


2  

super easy way:

超级简单的方法:

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
        }
    }
}

you just need to put your keys in the web/app.config file:

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

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

These can be obtained from you account page in the AWS console. They must use the names quoted here too, as they are pre-defined by the AWS library.

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