AWS 之 S3篇<.NET(c#)批量上传文件>
2016-01-21 16:25
591 查看
第一次知道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了。如下图
View Code
好了,其他方法调用就行
看下Web.config的配置
<add key="bucketName" value="bucketName"/>
<add key="filePath" value="D:\Log"/>
多加这2个,其他的还有问题,可以私心我……
现在开始讲讲我做这个的前后经过了。
首先呢,先要去官网下载一个SDK,网址http://aws.amazon.com/cn/developers/getting-started/net/。
下载好之后,就安装了,安装好之后,打开你的VS 新建项目时候就可以看到AWS了。如下图
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Amazon.S3; using Amazon.S3.Model; using System.Configuration; using System.IO; namespace AwsWebApp { public class Upload { public static string bucketName = ConfigurationManager.AppSettings["bucketName"]; public static string filePath = ConfigurationManager.AppSettings["filePath"]; public void UploadLogs() { //检查是否存在目的目录 if (Directory.Exists(filePath)) { List<FileInfo> listFiles = GetAllFilesInDirectory(filePath); string logFilePath = string.Empty; string Key = string.Empty; string result = string.Empty; foreach (FileInfo item in listFiles) { logFilePath = item.DirectoryName + "/" + item.Name; if (item.DirectoryName == filePath) { Key = item.Name; } else { Key = item.DirectoryName.Replace(filePath, "") + "/" + item.Name; Key = Key.Substring(1, Key.Length - 1).Replace("\\","/"); Key = Key.Replace("\\", "/"); } //判断文件是不是存在 if (File.Exists(logFilePath)) { if (UploadToS3(logFilePath, Key) == "OK") { //如果存在则删除 // File.Delete(logFilePath); } } } } } private List<FileInfo> GetAllFilesInDirectory(string strDirectory) { List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息 DirectoryInfo directory = new DirectoryInfo(strDirectory); DirectoryInfo[] directoryArray = directory.GetDirectories(); FileInfo[] fileInfoArray = directory.GetFiles(); if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray); foreach (DirectoryInfo _directoryInfo in directoryArray) { listFiles.AddRange(GetAllFilesInDirectory(_directoryInfo.FullName));//递归遍历 } return listFiles; } //执行上传 private string UploadToS3(string logFilePath, string Key) { try { IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); PutObjectRequest request = new PutObjectRequest() { BucketName = bucketName, Key = Key, FilePath = logFilePath }; PutObjectResponse response = client.PutObject(request); return response.HttpStatusCode.ToString(); } catch (AmazonS3Exception s3Exception) { Console.WriteLine(s3Exception.Message, s3Exception.InnerException); return ""; } } } }
View Code
好了,其他方法调用就行
看下Web.config的配置
<add key="bucketName" value="bucketName"/>
<add key="filePath" value="D:\Log"/>
多加这2个,其他的还有问题,可以私心我……
相关文章推荐
- C# 利用 OpenCV 进行视频捕获 (笔记)
- RickTextBox控件基础用法
- 伸展二叉树树(C#)
- C#的Enum——枚举
- C#获取当前日期时间(转)
- C#delegate委托
- c# 导入Excel 存到DataTable并进行行转列操作及合并DataTable相同行的值
- C# new用法总结
- C#创建、安装一个Windows服务
- C#数组初始化(全)
- C#反射
- C#中计算时间差
- C# - ref
- C# word 操作页脚书签
- C# 线程知识--使用Task执行异步操作
- C#中数据类型的基本了解
- string.Format对C#字符串格式化
- C# 改变图片尺寸(压缩),Image Resize
- C#内存分配学习
- C# MD5-16位加密实例,32位加密实例(两种方法)