400 086 0698

Language

知识干货

.net core AWS S3文件存储桶操作

发布时间:2020-07-24

浏览量:1064


S3存储桶操作有几种场景,
这里就只讲两种
一种是在EC2服务器上,使用 IAM 角色直接操作的
一种是通过账号密码,请求操作
其中S3文件有加密操作,可根据自己的需求而定

. EC2服务器上,使用 IAM 角色直接操作

这种情况是最简单的

由于在写程序的过程中,如果不是自己的S3存储桶,不能登录查看, 没办法直接看到存储桶内,文件上传下载情况,不好测试,这里教大家一个简单点的方法,直接使用cmd命令,查看情况。

CMD命令操作示例如下:

1.   以下命令直接输入:

2.   // 读取EC2服务器默认S3配置

3.       aws configure

4.   // 查看存储同或文件情况情况:

5.       aws s3 ls s3://***/

6.   // *** 这里直接写存储桶名称即可,也可以直接写到指定文件夹

7.   // 上传单个文件

8.       aws s3 cp C:/Users/test.txt s3://***/test/

9.   // 如果没有S3没有test文件夹,会自动创建

10.  // 下载文件,反过来写即可

11.      aws s3 cp C:/Users/ s3://***/test/test.txt

12.  // 删除文件

13.      aws s3 rm s3://xxx/test/ --recursive

有了这些命令之后,在调成程序的过程中,我们就能更好的查看文件的操作效果了,接下来,正式进入AWS S3 ,.NET Core的程序编写

1.首先我们需要用NuGet安装一些拓展

AWSSDK.S3
AWSSDK.Extensions.NETCore.Setup

 

 

 

后面直接上代码:

Startup.cs文件配置,添加如下代码,其余自行填充:

1.       public Startup(IConfiguration configuration, IHostingEnvironment env)

2.       {

3.           var builder = new ConfigurationBuilder()

4.               .SetBasePath(env.ContentRootPath)

5.               .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

6.               .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)

7.               .AddEnvironmentVariables();

8.           Configuration = builder.Build();

9.       }

10.   

11.      public void ConfigureServices(IServiceCollection services)

12.      {

13.          IAmazonS3 client = Configuration.GetAWSOptions().CreateServiceClient<IAmazonS3>();

14.          // 存储桶名称 如:s3://***/,直接填写***

15.          AwsStorageClient.BucketName = "***";

16.          AwsStorageClient.Storage = new AwsStorageHelper(client);

17.          services.AddDefaultAWSOptions(Configuration.GetAWSOptions());

18.          services.AddAWSService<IAmazonS3>();

19.          services.AddOptions();

20.      }

21.   

22.   

23.      public void Configure(IApplicationBuilder app, IHostingEnvironment env)

24.      {

25.          var options = Configuration.GetAWSOptions();

26.          IAmazonS3 client = options.CreateServiceClient<IAmazonS3>();

27.      }

AwsStorageHelper.cs 文件内容:

1.       public class AwsStorageHelper

2.       {

3.           IAmazonS3 S3Client { get; set; }

4.    

5.           public static string BaseURL { get; set; }

6.    

7.           public AwsStorageHelper(IAmazonS3 s3Client)

8.           {

9.               S3Client = s3Client;

10.          }

11.   

12.          /// <summary>

13.          /// Get S3 Client for connecting to S3

14.          /// </summary>

15.          /// <returns>S3 Client</returns>

16.          public IAmazonS3 GetClient()

17.          {

18.              if (S3Client != null)

19.              {

20.                  return S3Client;

21.              }

22.              //为了确保不出意外,防止为null,重新实例化

23.              var config = new AmazonS3Config { ServiceURL = BaseURL };

24.   

25.              return S3Client = new AmazonS3Client(config);

26.          }

27.      }

AwsStorageClient.cs 文件操作底层封装方法,简单的上传,下载,删除:

1.       public class AwsStorageClient

2.       { /// <summary>

3.         /// Storage Helper - this is injected by Windsor. Provides the S3 client.

4.         /// </summary>

5.           public static AwsStorageHelper Storage { getset; }

6.           /// <summary>

7.           /// Bucket Name - this must be set before use.

8.           /// </summary>

9.           public static string BucketName { getset; }

10.          /// <summary>

11.          /// 验证文件是否存在

12.          /// </summary>

13.          /// <param name="filePath"></param>

14.          /// <returns></returns>

15.          public bool Exists(string filePath)

16.          {

17.              EnsureBucketName();

18.              filePath = CleanFilePath(filePath);

19.              var client = Storage.GetClient();

20.              ListObjectsV2Request request = new ListObjectsV2Request

21.              {

22.                  BucketName = BucketName,

23.              };

24.              //var s3DirectoryInfo = new S3DirectoryInfo(client, BucketName);

25.              ListObjectsV2Response response = client.ListObjectsV2Async(request).Result;

26.              return response.S3Objects.Exists(c => c.Key == filePath);

27.          }

28.          

29.          public void WriteFile(string filePath, Stream fileContent)

30.          {

31.              EnsureBucketName();

32.              filePath = CleanFilePath(filePath);

33.              var client = Storage.GetClient();

34.              using (var uploader = new TransferUtility(client))

35.              {

36.                  uploader.Upload(fileContent, BucketName, filePath);

37.              }

38.          }

39.          

40.          public long FileSize(string filePath)

41.          {

42.              EnsureBucketName();

43.              filePath = CleanFilePath(filePath);

44.              var client = Storage.GetClient();

45.              using (var response = client.ListObjectsAsync(new ListObjectsRequest { BucketName = BucketNamePrefix = filePath }))

46.              {

47.                  if (response.Result.S3Objects.Count > 0)

48.                  {

49.                      return response.Result.S3Objects.First().Size;

50.                  }

51.                  return 0;

52.              }

53.          }

54.          public string FileType(string filePath)

55.          {

56.              return "application/octet-stream";

57.          }

58.          /// <summary>

59.          /// 上传文件

60.          /// </summary>

61.          /// <param name="base64Key"></param>

62.          /// <param name="filePath"></param>

63.          /// <returns></returns>

64.          public async Task<PutObjectRequestUploadObjectAsync(string base64Key, string filePath)

65.          {

66.              var client = Storage.GetClient();

67.              PutObjectRequest putObjectRequest = new PutObjectRequest

68.              {

69.                  BucketName = BucketName,

70.                  Key = filePath,

71.                  ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,// 文件加密,可不要

72.                  ServerSideEncryptionCustomerProvidedKey = base64Key,// 文件加密,可不要

73.              };

74.              PutObjectResponse putObjectResponse = await client.PutObjectAsync(putObjectRequest);

75.              return putObjectRequest;

76.          }

77.          

78.          /// <summary>

79.          /// 读取文件

80.          /// </summary>

81.          /// <param name="filePath"></param>

82.          /// <param name="base64Key"></param>

83.          /// <returns></returns>

84.          public Stream ReadFile(string filePath,string base64Key)

85.          {

86.              EnsureBucketName();

87.              filePath = CleanFilePath(filePath);

88.              var client = Storage.GetClient();

89.              GetObjectRequest request = new GetObjectRequest()

90.              {

91.                  BucketName = BucketName,

92.                  Key = filePath,

93.                  ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,// 文件加密,可不要

94.                  ServerSideEncryptionCustomerProvidedKey = base64Key// 文件加密,可不要

95.              };

96.              var response = client.GetObjectAsync(request);

97.              return response.Result.ResponseStream;

98.          }

99.          private void EnsureBucketName()

100.        {

101.            if (BucketName == string.Empty)

102.            {

103.                throw new Exception("Bucket Name must be set before using the S3 storage client");

104.            }

105.        }

106.        private string CleanFilePath(string path)

107.        {

108.            // remove leading slashes

109.            if (path.Length > 0 && path.Substring(01) == "/")

110.            {

111.                path = Regex.Replace(path, "^[/]+(.*)$""$1");

112.            }

113.            return path;

114.        }

115.        public class S3Response

116.        {

117.            public HttpStatusCode Status { getset; }

118.            public string Message { getset; }

119.        }

120.        

121.        /// <summary>

122.        /// 删除一个对象

123.        /// </summary>

124.        /// <param name="key">删除的对象的键如:resource/img/basketballnews/test1.jpg</param>

125.        /// <returns></returns>

126.        public async Task<bool> DeleteAnObjectAsync(string keyName)

127.        {

128.            try

129.            {

130.                // 1. Delete object-specify only key name for the object.

131.                var deleteRequest1 = new DeleteObjectRequest

132.                {

133.                    BucketName = BucketName,

134.                    Key = keyName

135.                };

136.                var client = Storage.GetClient();

137.                DeleteObjectResponse response1 = await client.DeleteObjectAsync(deleteRequest1);

138.                return true;

139.            }

140.            catch (AmazonS3Exception e)

141.            {

142.                throw new Exception(string.Format("Error encountered ***. Message:'{0}' when writing an object", e.Message));

143.            }

144.            catch (Exception e)

145.            {

146.                throw new Exception(string.Format("Unknown encountered on server. Message:'{0}' when writing an object", e.Message));

147.            }

148.        }

149.    }

最后就是控制器层的调用了,AWSController.cs

1.       [Produces("application/json")]

2.       [Consumes("application/json""multipart/form-data")]//此处为新增

3.       [Route("api/[controller]/[action]")]

4.       public class AWSController : Controller

5.       {

6.           IAmazonS3 S3Client { getset; }

7.           public AWSController(IAmazonS3 s3Client)

8.           {

9.               this.S3Client = s3Client;

10.          }

11.          /// <summary>

12.          /// S3 存储桶 异步删除文件

13.          /// </summary>

14.          /// <returns></returns>

15.          [HttpPost]

16.          public async Task<IActionResultDeleteFilesS3Async()

17.          {

18.              AwsStorageClient aws = new AwsStorageClient();

19.              string awsfilePath = "s3Test/text/text.txt";

20.              await aws.DeleteAnObjectAsync(awsfilePath);

21.              return Ok();

22.          }

23.          /// <summary>

24.          /// S3 存储桶 上传文件

25.          /// </summary>

26.          /// <returns></returns>

27.          [HttpPost]

28.          [DisableRequestSizeLimit]

29.          public async Task<ActionResult<bool>UploadFileS3(IFormFile file)

30.          {

31.              string awsfilePath = "s3Test/test/" + file.FileName;// 上传 AWS 文件路径,文件名全路径,带后缀

32.              AwsStorageClient aws = new AwsStorageClient();

33.              string base64Key = "none";

34.              // 文件是否需要加密根据自己情况而定

35.              Aes aesEncryption = Aes.Create();

36.              aesEncryption.KeySize = 256;

37.              aesEncryption.GenerateKey();

38.              base64Key = Convert.ToBase64String(aesEncryption.Key);

39.              await aws.UploadObjectAsync(base64Key, awsfilePath);

40.              return ActionResultUtil.JsonText(base64Key);

41.          }

42.   

43.          /// <summary>

44.          /// S3 存储桶 下载文件

45.          /// </summary>

46.          /// <returns></returns>

47.          [HttpPost]

48.          public ActionResult<StreamDownFileS3([FromBody]string base64Key, string filePath)

49.          {

50.              AwsStorageClient aws = new AwsStorageClient();

51.              // filePath = "s3Test/test/001.png";

52.              bool res = aws.Exists(filePath);

53.              try

54.              {

55.                  if (res)

56.                      return aws.ReadFile(filePath, base64Key);

57.                  else

58.                      return NotFound();

59.              }

60.              catch (Exception)

61.              {

62.                  return NotFound();

63.              }

64.          }

65.      }

 

以上就是第一种的操作解决方案,相比第二种,它没什么重要配置。 而第二种需要配置一些相关信息,通过账号,密码去访问

 

 

. 通过 AccessKey SecretKey登录操作

底层操作基本一致,只需要配置好相关的key就行, 我这里就直接在Startup.cs中进行登录, 其他代码,基本与方案一一致,可直接使用方案一代码程序。

1.       public Startup(IConfiguration configuration, IHostingEnvironment env)

2.       {

3.           var builder = new ConfigurationBuilder()

4.               .SetBasePath(env.ContentRootPath)

5.               .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

6.               .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)

7.               .AddEnvironmentVariables();

8.           Configuration = builder.Build();

9.       }

10.   

11.   

12.      public void ConfigureServices(IServiceCollection services)

13.      {

14.          IAmazonS3 client = Configuration.GetAWSOptions().CreateServiceClient<IAmazonS3>();

15.          // 存储桶名称 如:s3://***/,直接填写***

16.          AwsStorageClient.BucketName = "***";

17.          AwsStorageHelper.BaseURL = Configuration["AWS:BaseURL"]; // s3服务器地址,一般都直接使用的“s3.amazonaws.com” ,不配置也没什么问题

18.          AmazonS3Config config = new AmazonS3Config()

19.          {

20.              ServiceURL = AwsStorageHelper.BaseURL,

21.              RegionEndpoint = RegionEndpoint.APNortheast1,

22.          };

23.          AwsStorageClient.Storage =

24.              new AwsStorageHelper(

25.              new AmazonS3Client(Configuration["AWS:AccessKey"], Configuration["AWS:SecretKey"], config));

26.          AwsStorageClient.Storage = new AwsStorageHelper();

27.          services.AddDefaultAWSOptions(Configuration.GetAWSOptions());

28.          services.AddAWSService<IAmazonS3>();

29.          services.AddOptions();

30.      }

31.   

32.      public void Configure(IApplicationBuilder app, IHostingEnvironment env)

33.      {

34.          var options = Configuration.GetAWSOptions();

35.          IAmazonS3 client = options.CreateServiceClient<IAmazonS3>();

36.        }


需要查阅更多资源?