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個,其他的還有問題,可以私心我……

 

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM