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
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(, Key.Length - ).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 > ) 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 "";
}
}
}
}
好了,其他方法调用就行
看下Web.config的配置
<add key="bucketName" value="bucketName"/>
<add key="filePath" value="D:\Log"/>
多加这2个,其他的还有问题,可以私心我……
最新文章
- 深入学习jQuery选择器系列第二篇——过滤选择器之子元素选择器
- 修改AndroidStudio中的Logcat中的默认设置
- 阿里云centos试用
- Caffe学习系列(19): 绘制loss和accuracy曲线
- iOS中plist的创建,数据写入与读取
- Bzoj3893 [Usaco2014 Dec]Cow Jog
- mysql 学习(1)
- linux命令和知识点
- 整数n的全排列
- 继续写java和socket
- C#轻量级通通讯组件StriveEngine —— C/S通信开源demo(2) —— 使用二进制协议 (附源码)
- 慢查询日志工具mysqlsla的使用
- Ajax框架---dwr的用法
- DVWA渗透测试系列 一 (DVWA环境配置)
- 把 ElasticSearch 当成是 NoSQL 数据库
- 记录一个python公式罗列的方法 join()方法和map()方法的妙用
- Windows 8.1 ";计算机"; 中文件夹清理
- Python已成为网络攻击的首选编程语言
- 每天一个linux命令:【转载】less命令
- java(一)IntelliJ和eclipse环境下的Hello World