C# 如何使用c将文件上传到AmazonS3超级简单#
我厌倦了所有这些“上传到S3”的示例和教程,有人能给我看一个简单易行的示例吗?好的,下面是你必须遵循的说明,以获得一个完全有效的演示程序 1-下载并安装Amazon web services SDK for.NET,可在()中找到。因为我有VisualStudio2010,所以我选择安装3.5.NETSDK 2-打开visual studio并创建一个新项目,我有visual studio 2010,我正在使用一个控制台应用程序项目 3-添加对AWSSDK.dll的引用,它与上述Amazon web service SDK一起安装,在我的系统中,该dll位于“C:\Program Files(x86)\AWS SDK for.NET\bin\Net35\AWSSDK.dll”中 4-创建一个新的类文件,称之为“AmazonPloader”,这里是类的完整代码:C# 如何使用c将文件上传到AmazonS3超级简单#,c#,.net,amazon-s3,C#,.net,Amazon S3,我厌倦了所有这些“上传到S3”的示例和教程,有人能给我看一个简单易行的示例吗?好的,下面是你必须遵循的说明,以获得一个完全有效的演示程序 1-下载并安装Amazon web services SDK for.NET,可在()中找到。因为我有VisualStudio2010,所以我选择安装3.5.NETSDK 2-打开visual studio并创建一个新项目,我有visual studio 2010,我正在使用一个控制台应用程序项目 3-添加对AWSSDK.dll的引用,它与上述Amazon w
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 (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”的文件被添加到解决方案中
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>
8-用您自己的数据替换上面代码中的字符串
9-添加错误更正
您的程序已经准备就绪,@docesam的解决方案适用于旧版本的AWSSDK下面是AmazonS3的最新文档示例:
IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);
FileInfo file = new FileInfo(@"c:\test.txt");
string destPath = "folder/sub-folder/test.txt"; // <-- low-level s3 path uses /
PutObjectRequest request = new PutObjectRequest()
{
InputStream = file.OpenRead(),
BucketName = "my-bucket-name",
Key = destPath // <-- in S3 key represents a path
};
PutObjectResponse response = client.PutObject(request);
IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);
FileInfo localFile = new FileInfo(@"c:\test.txt");
string destPath = @"folder\sub-folder\test.txt"; // <-- high-level s3 path uses \
S3FileInfo s3File = new S3FileInfo(client, "my-bucket-name", destPath);
if (!s3File.Exists)
{
using (var s3Stream = s3File.Create()) // <-- create file in S3
{
localFile.OpenRead().CopyTo(s3Stream); // <-- copy the content to S3
}
}
1) 首先打开Visual Studio(我正在使用VS2015)并创建一个新项目->ASP.NET Web应用程序->MVC
2) 在Manage Nuget Package中浏览该软件包AWSSDK.S3,然后安装它
3) 现在创建一个名为AmazonS3Uploader
的类,然后复制并粘贴此代码:
using System;
using Amazon.S3;
using Amazon.S3.Model;
namespace AmazonS3Demo
{
public class AmazonS3Uploader
{
private string bucketName = "your-amazon-s3-bucket";
private string keyName = "the-name-of-your-file";
private string filePath = "C:\\Users\\yourUserName\\Desktop\\myImageToUpload.jpg";
public void UploadFile()
{
var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
try
{
PutObjectRequest putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
FilePath = filePath,
ContentType = "text/plain"
};
PutObjectResponse response = client.PutObject(putRequest);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
throw new Exception("Check the provided AWS Credentials.");
}
else
{
throw new Exception("Error occurred: " + amazonS3Exception.Message);
}
}
}
}
}
4) 编辑您的Web.config文件,在
@mejiamanuel57的解决方案中添加下一行内容,对于15MB以下的小文件来说效果很好。对于较大的文件,我得到了System.Net.Sockets.SocketException:由于线程退出或应用程序请求
,I/O操作已中止。以下改进的解决方案适用于较大的文件(使用50MB文件测试):
。。。
public void UploadFile()
{
var client=newamazons3client(Amazon.RegionEndpoint.USEast1);
var transferUtility=新的transferUtility(客户);
尝试
{
TransferUtilityUploadRequest TransferUtilityUploadRequest=新TransferUtilityUploadRequest
{
BucketName=BucketName,
Key=keyName,
FilePath=FilePath,
ContentType=“text/plain”
};
transferUtility.Upload(transferUtilityUploadRequest);//如果可能,请使用UploadAsync
}
...
更多信息。我已经写了一篇关于这方面的文章
使用低级API将文件上载到S3存储桶:
IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);
FileInfo file = new FileInfo(@"c:\test.txt");
string destPath = "folder/sub-folder/test.txt"; // <-- low-level s3 path uses /
PutObjectRequest request = new PutObjectRequest()
{
InputStream = file.OpenRead(),
BucketName = "my-bucket-name",
Key = destPath // <-- in S3 key represents a path
};
PutObjectResponse response = client.PutObject(request);
IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);
FileInfo localFile = new FileInfo(@"c:\test.txt");
string destPath = @"folder\sub-folder\test.txt"; // <-- high-level s3 path uses \
S3FileInfo s3File = new S3FileInfo(client, "my-bucket-name", destPath);
if (!s3File.Exists)
{
using (var s3Stream = s3File.Create()) // <-- create file in S3
{
localFile.OpenRead().CopyTo(s3Stream); // <-- copy the content to S3
}
}
IAmazonS3客户端=新的Amazons3客户端(“AKI…访问密钥…”,“+8Bo…secrey密钥…”,”,RegionEndpoint.APSoutheast2);
FileInfo file=newfileinfo(@“c:\test.txt”);
string destPath=“folder/sub folder/test.txt”;//AWS站点上的示例对我有用:
尽管已将其设置为返回错误的其他区域:
//专用静态只读RegionEndpoint bucketRegion=RegionEndpoint.USWest2;
专用静态只读RegionEndpoint bucketRegion=RegionEndpoint.USWest1
我在北加利福尼亚州设置了我的bucket,即USWest1。谢谢您的回答,但在本例中,返回true表示操作成功是可怕的,不必要的。步骤1和3可以通过使用NuGet获取AWSSDK来代替。下载链接已断开。此答案有点过时,AWSSDK是容纳所有Aws l的大型组件在Aws 3.0中,对于您想要使用的Aws的每个功能,都有许多较小的程序集。Awssdk.s3可能是您想要使用的,而不是Awssdk。虽然部分API已经更改,但我认为Awssdk仍然存在。您如何获得响应数据,即对象的URL以及解决方案如何压缩图像,然后在更新后在服务器上加载图像FilePath=FilePath在这里有什么想法吗?我已经尝试了你的代码,它的工作很好,但我需要在上传图像之前压缩,然后在上传图像之后压缩,但如何才能做到这一点我不知道,所以请你帮我重播一下,你的解决方案是非常好的,我已经尝试过了,做得很好。无法访问PutObject method由于其保护级别此代码针对哪个版本进行测试?我在csproj文件()中有此代码,它给出了错误:错误CS0122:“AmazonS3Client.PutObject(PutObjectRequest)”由于其保护级别不可访问尼斯,很长时间没有使用我自己的方法,但我将尝试您的解决方案。谢谢!
IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);
FileInfo localFile = new FileInfo(@"c:\test.txt");
string destPath = @"folder\sub-folder\test.txt"; // <-- high-level s3 path uses \
S3FileInfo s3File = new S3FileInfo(client, "my-bucket-name", destPath);
if (!s3File.Exists)
{
using (var s3Stream = s3File.Create()) // <-- create file in S3
{
localFile.OpenRead().CopyTo(s3Stream); // <-- copy the content to S3
}
}