aws python sdk批量上传_AWS 之 S3篇<.NET(c#)批量上传文件>

第一次知道AWS然后网上就研究了一番,如果也是第一次知道这个的话,或者对这个只知道是干什么,并没有写个相关的程序的话,可以看看这个网址http://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/Welcome.html。

现在开始讲讲我做这个的前后经过了。

首先呢,先要去官网下载一个SDK,网址http://aws.amazon.com/cn/developers/getting-started/net/。

下载好之后,就安装了,安装好之后,打开你的VS 新建项目时候就可以看到AWS了。如下图

选择想要新建的项目,建议新手创建Web项目,原因嘛,调试方便,确定后出现如下:

输入你的登录名 ,账号了,密码了等等……然后点击OK。

新建的项目出来了,有可能新建的项目AWS的引用报警,你只需要重新引入下就行,哪里有呢?就在你刚刚安装SDK 的路径那里面找,有3.5和4.5的,所以你的项目最低版本是3.5.

引入好,然后生成,不报错,然后F5启动下。

如果出现下图:

恭喜你,新建项目成功。

为什么我其他两个都有error呢,那是因为我这个账号,没有其他2个的权限。

如果你的项目F5出来不是这样,那就看你的报什么错了。(如果我不忙的话,到时候给你看下,你也可以自己研究)

然后开始做上传。

新建类Upload.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Web;5 usingAmazon.S3;6 usingAmazon.S3.Model;7 usingSystem.Configuration;8 usingSystem.IO;9

10 namespaceAwsWebApp11 {12 public classUpload13 {14 public static string bucketName = ConfigurationManager.AppSettings["bucketName"];15 public static string filePath = ConfigurationManager.AppSettings["filePath"];16

17 public voidUploadLogs()18 {19 //检查是否存在目的目录

20 if(Directory.Exists(filePath))21 {22 List listFiles =GetAllFilesInDirectory(filePath);23 string logFilePath = string.Empty;24 string Key = string.Empty;25 string result = string.Empty;26 foreach (FileInfo item inlistFiles)27 {28 logFilePath = item.DirectoryName + "/" +item.Name;29 if (item.DirectoryName ==filePath)30 {31 Key =item.Name;32 }33 else

34 {35 Key = item.DirectoryName.Replace(filePath, "") + "/" +item.Name;36 Key = Key.Substring(1, Key.Length - 1).Replace("\\","/");37 Key = Key.Replace("\\", "/");38 }39 //判断文件是不是存在

40 if(File.Exists(logFilePath))41 {42 if (UploadToS3(logFilePath, Key) == "OK")43 {44 //如果存在则删除45 //File.Delete(logFilePath);

46

47 }48 }49 }50 }51

52 }53 private List GetAllFilesInDirectory(stringstrDirectory)54 {55 List listFiles = new List(); //保存所有的文件信息

56 DirectoryInfo directory = newDirectoryInfo(strDirectory);57 DirectoryInfo[] directoryArray =directory.GetDirectories();58 FileInfo[] fileInfoArray =directory.GetFiles();59 if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray);60 foreach (DirectoryInfo _directoryInfo indirectoryArray)61 {62 listFiles.AddRange(GetAllFilesInDirectory(_directoryInfo.FullName));//递归遍历

63 }64 returnlistFiles;65 }66 //执行上传

67 private string UploadToS3(string logFilePath, stringKey)68 {69 try

70 {71 IAmazonS3 client = newAmazonS3Client(Amazon.RegionEndpoint.USEast1);72 PutObjectRequest request = newPutObjectRequest()73 {74 BucketName =bucketName,75 Key =Key,76 FilePath =logFilePath77 };78 PutObjectResponse response =client.PutObject(request);79 returnresponse.HttpStatusCode.ToString();80 }81 catch(AmazonS3Exception s3Exception)82 {83 Console.WriteLine(s3Exception.Message, s3Exception.InnerException);84 return "";85 }86 }87 }88 }

View Code

好了,其他方法调用就行

看下Web.config的配置

多加这2个,其他的还有问题,可以私心我……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值