如何使用 C# 将文件上传到亚马逊 S3 超级简单 [英] How to upload a file to amazon S3 super easy using c#
问题描述
我厌倦了所有这些不起作用的上传到 S3"示例和教程,有人可以向我展示一个简单有效且超级简单的示例吗?
I am tired of all these "upload to S3" examples and tutorials that don't work , can someone just show me an example that simply works and is super easy?
推荐答案
这里是您必须遵循的说明才能获得一个完整的演示程序......
well here are the instruction that you have to follow to get a fully working demo program ...
1-下载并安装适用于 .NET 的亚马逊网络服务 SDK,您可以在 (http://aws.amazon.com/sdk-for-net/).因为我有 Visual Studio 2010,所以我选择安装 3.5 .NET SDK.
1-Download and install the Amazon web services SDK for .NET which you can find in (http://aws.amazon.com/sdk-for-net/). because I have visual studio 2010 I choose to install the 3.5 .NET SDK.
2- 打开 Visual Studio 并创建一个新项目,我有 Visual Studio 2010 并且我正在使用控制台应用程序项目.
2- open visual studio and make a new project , I have visual studio 2010 and I am using a console application project.
3- 添加对 AWSSDK.dll 的引用,它与上面提到的 Amazon Web Service SDK 一起安装,在我的系统中,dll 位于C:Program Files (x86)AWS SDK for .NETin"Net35AWSSDK.dll".
3- add reference to AWSSDK.dll , it is installed with the Amazon web service SDK mentioned above , in my system the dll is located in "C:Program Files (x86)AWS SDK for .NETinNet35AWSSDK.dll".
4- 创建一个新的类文件,在此处将其命名为AmazonUploader" 类的完整代码:
4- make a new class file ,call it "AmazonUploader" here the complete code of the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
namespace UploadToS3Demo
{
public class AmazonUploader
{
public bool sendMyFileToS3(string localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
{
// input explained :
// localFilePath = the full local file path e.g. "c:mydirmysubdirmyfilename.zip"
// bucketName : the name of the bucket in S3 ,the bucket should be alreadt created
// subDirectoryInBucket : if this string is not empty the file will be uploaded to
// a subdirectory with this name
// fileNameInS3 = the file name in the S3
// create an instance of IAmazonS3 class ,in my case i choose RegionEndpoint.EUWest1
// you can change that to APNortheast1 , APSoutheast1 , APSoutheast2 , CNNorth1
// SAEast1 , USEast1 , USGovCloudWest1 , USWest1 , USWest2 . this choice will not
// store your file in a different cloud storage but (i think) it differ in performance
// depending on your location
IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.EUWest1);
// create a TransferUtility instance passing it the IAmazonS3 created in the first step
TransferUtility utility = new TransferUtility(client);
// making a TransferUtilityUploadRequest instance
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
{
request.BucketName = bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
request.BucketName = bucketName + @"/" + subDirectoryInBucket;
}
request.Key = fileNameInS3 ; //file name up in S3
request.FilePath = localFilePath; //local file name
utility.Upload(request); //commensing the transfer
return true; //indicate that the file was sent
}
}
}
5- 添加配置文件:在解决方案资源管理器中右键单击您的项目并选择添加"->新项目",然后从列表中选择应用程序配置文件"类型并单击添加"按钮.一个名为App.config"的文件被添加到解决方案中.
5- add a configuration file : right click on your project in the solution explorer and choose "add" -> "new item" then from the list choose the type "Application configuration file" and click the "add" button. a file called "App.config" is added to the solution.
6- 编辑 app.config 文件:双击解决方案资源管理器中的app.config"文件,将出现编辑菜单.用以下文本替换所有文本:
6- edit the app.config file : double click the "app.config" file in the solution explorer the edit menu will appear . replace all the text with the following text :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AWSProfileName" value="profile1"/>
<add key="AWSAccessKey" value="your Access Key goes here"/>
<add key="AWSSecretKey" value="your Secret Key goes here"/>
</appSettings>
</configuration>
您必须修改上述文本以反映您的 Amazon Access Key Id 和 Secret Access Key.
you have to modify the above text to reflect your Amazon Access Key Id and Secret Access Key.
7- 现在在 program.cs 文件中(记住这是一个控制台应用程序)编写以下代码:
7- now in the program.cs file (remember this is a console application) write the following code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UploadToS3Demo
{
class Program
{
static void Main(string[] args)
{
// preparing our file and directory names
string fileToBackup = @"d:mybackupFile.zip" ; // test file
string myBucketName = "mys3bucketname"; //your s3 bucket name goes here
string s3DirectoryName = "justdemodirectory";
string s3FileName = @"mybackupFile uploaded in 12-9-2014.zip";
AmazonUploader myUploader = new AmazonUploader();
myUploader.sendMyFileToS3(fileToBackup, myBucketName, s3DirectoryName, s3FileName);
}
}
}
8- 用你自己的数据替换上面代码中的字符串
8- replace the strings in the code above with your own data
9- 添加纠错你的程序已经准备好了
9- add error correction and your program is ready
这篇关于如何使用 C# 将文件上传到亚马逊 S3 超级简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!