我厌倦了所有这些不起作用的“上传到 S3”示例和教程,有人能给我一个简单有效且 super 简单的示例吗?
最佳答案
好吧,这里是您必须遵循的说明才能获得完整的演示程序...
1 - 下载并安装适用于 .NET 的亚马逊网络服务 SDK,您可以在 ( http://aws.amazon.com/sdk-for-net/ ) 中找到它。因为我有 visual studio 2010,所以我选择安装 3.5 .NET SDK。
2- 打开 visual studio 并创建一个新项目,我有 visual studio 2010,我正在使用控制台应用程序项目。
3- 添加对 AWSSDK.dll 的引用,它与上面提到的 Amazon Web 服务 SDK 一起安装,在我的系统中,dll 位于“C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.dll"。
4- 制作一个新的类文件,将其命名为“AmazonUploader”,这里是该类的完整代码:
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:\mydir\mysubdir\myfilename.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 (string.IsNullOrEmpty(subDirectoryInBucket))
{
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”的文件被添加到解决方案中。
6- 编辑 app.config 文件:双击解决方案资源管理器中的“app.config”文件,将出现编辑菜单。用以下文本替换所有文本:
<?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>
您必须修改以上文本以反射(reflect)您的 Amazon 访问 key ID 和 secret 访问 key 。
7- 现在在 program.cs 文件中(记住这是一个控制台应用程序)写入以下代码:
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-用自己的数据替换上面代码中的字符串
9-添加纠错 并且您的程序已准备就绪
关于c# - 如何使用 C# 轻松上传文件到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25814972/