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 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using Amazon.S3;
 6 using Amazon.S3.Model;
 7 using System.Configuration;
 8 using System.IO;
 9 
10 namespace AwsWebApp
11 {
12     public class Upload
13     {
14         public static string bucketName = ConfigurationManager.AppSettings["bucketName"];
15         public static string filePath = ConfigurationManager.AppSettings["filePath"];
16 
17         public void UploadLogs()
18         {
19             //检查是否存在目的目录  
20             if (Directory.Exists(filePath))
21             {
22                 List<FileInfo> listFiles = GetAllFilesInDirectory(filePath);
23                 string logFilePath = string.Empty;
24                 string Key = string.Empty;
25                 string result = string.Empty;
26                 foreach (FileInfo item in listFiles)
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<FileInfo> GetAllFilesInDirectory(string strDirectory)
54         {
55             List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息  
56             DirectoryInfo directory = new DirectoryInfo(strDirectory);
57             DirectoryInfo[] directoryArray = directory.GetDirectories();
58             FileInfo[] fileInfoArray = directory.GetFiles();
59             if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray);
60             foreach (DirectoryInfo _directoryInfo in directoryArray)
61             {
62                listFiles.AddRange(GetAllFilesInDirectory(_directoryInfo.FullName));//递归遍历  
63             }
64             return listFiles;
65         }
66     //执行上传
67         private string UploadToS3(string logFilePath, string Key)
68         {
69             try
70             {
71                 IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
72                 PutObjectRequest request = new PutObjectRequest()
73                 {
74                     BucketName = bucketName,
75                     Key = Key,
76                     FilePath = logFilePath
77                 };
78                 PutObjectResponse response = client.PutObject(request);
79                 return response.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的配置

  <add key="bucketName" value="bucketName"/>
  <add key="filePath" value="D:Log"/>

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

  

  

原文地址:https://www.cnblogs.com/lvphon/p/5148702.html