如何在WinForm项目中使用AWS S3

编程入门 行业动态 更新时间:2024-10-27 05:25:13
如何在WinForm项目中使用AWS S3 - CSharp(How to use AWS S3 in WinForm project - CSharp)

我一直在阅读有关在Visual Studio中使用AWS S3的多篇文章。 但所有这些都集中在Web实现上。

有没有人需要在WinForms中实现AWS S3 - C#?

I'm been reading multiples articles about the usage of the AWS S3 in Visual Studio. But all of those are focused on Web implementation.

Has anyone had the need to implement AWS S3 in WinForms - C#?

最满意答案

我在C#中实现了一个基本与AWS S3交互的类,而不需要太多配置。

**我正在使用MS Framework 4.5 **

首先,请记住使用NuGet包并安装AWS参考(在VS >>菜单>>工具>> NuGet包管理器>>管理NuGet包管理器以获得解决方案):

AWSSDK - 亚马逊简单存储服务 AWSSDK - 核心运行时

...实现了这个类,为了我的需要,我只需要访问密钥,密钥,区域和存储桶(存在更多方法,但我只需要复制,读取和删除)。

注意:在app.config文件中不需要额外的配置,或者在USER文件夹中安装/创建配置文件...

public class clsAwsS3 { string accessKey { get; set; } string secretKey { get; set; } string bucket { get; set; } RegionEndpoint region { get; set; } IAmazonS3 client; public clsAwsS3(string strBucket, string strAccessKey, string strSecretKey, RegionEndpoint region) { this.bucket = strBucket; this.accessKey = strAccessKey; this.secretKey = strSecretKey; this.region = region; login(); } private void login() { client = new AmazonS3Client(accessKey, secretKey, region); } public List<string> getItems(string strPrefix = "") { List<string> lstResult = new List<string>(); ListObjectsV2Request listRequest; if (strPrefix == "") listRequest = new ListObjectsV2Request { BucketName = bucket }; else listRequest = new ListObjectsV2Request { BucketName = bucket, Prefix = strPrefix }; ListObjectsV2Response listResponse; do { listResponse = client.ListObjectsV2(listRequest); foreach (S3Object awsObject in listResponse.S3Objects) lstResult.Add(awsObject.Key); listRequest.ContinuationToken = listResponse.NextContinuationToken; } while (listResponse.IsTruncated); return lstResult; } public string downloadItem(string strItemKey, string strDestination) { GetObjectRequest request = new GetObjectRequest { BucketName = bucket, Key = strItemKey }; using (GetObjectResponse response = client.GetObject(request)) { response.WriteResponseStreamToFile(strDestination); } return strDestination; } public void copyItem(string strItemKeySource, string strItemKeyDestination) { CopyObjectRequest copyRequest = new CopyObjectRequest { SourceBucket = bucket, SourceKey = strItemKeySource, DestinationBucket = bucket, DestinationKey = strItemKeyDestination }; CopyObjectResponse copyResponse = client.CopyObject(copyRequest); if (copyResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) throw new Exception("Item has an error"); } public void deleteItem(string strItemKey) { DeleteObjectRequest deleteObject = new DeleteObjectRequest { BucketName = bucket, Key = strItemKey }; DeleteObjectResponse deleteResponse = client.DeleteObject(deleteObject); } }

希望这有助于其他人。

I implemented a class in C# that interact basically with AWS S3 without having too much configuration.

** I'm using the MS Framework 4.5 **

First of all, remember to use the NuGet Package and install the AWS references (In VS >> Menu >> Tools >> NuGet Package Manager >> Manage NuGet Package Manager for Solution):

AWSSDK - Amazon Simple Storage Service AWSSDK - Core Runtime

...implemented this class, for my need I only required the access key, secret key, region and the bucket (Exists more methods, but I only required copy, read and delete).

Note: It's not required additional configuration in the app.config file or install/create a profile in the USER folder...

public class clsAwsS3 { string accessKey { get; set; } string secretKey { get; set; } string bucket { get; set; } RegionEndpoint region { get; set; } IAmazonS3 client; public clsAwsS3(string strBucket, string strAccessKey, string strSecretKey, RegionEndpoint region) { this.bucket = strBucket; this.accessKey = strAccessKey; this.secretKey = strSecretKey; this.region = region; login(); } private void login() { client = new AmazonS3Client(accessKey, secretKey, region); } public List<string> getItems(string strPrefix = "") { List<string> lstResult = new List<string>(); ListObjectsV2Request listRequest; if (strPrefix == "") listRequest = new ListObjectsV2Request { BucketName = bucket }; else listRequest = new ListObjectsV2Request { BucketName = bucket, Prefix = strPrefix }; ListObjectsV2Response listResponse; do { listResponse = client.ListObjectsV2(listRequest); foreach (S3Object awsObject in listResponse.S3Objects) lstResult.Add(awsObject.Key); listRequest.ContinuationToken = listResponse.NextContinuationToken; } while (listResponse.IsTruncated); return lstResult; } public string downloadItem(string strItemKey, string strDestination) { GetObjectRequest request = new GetObjectRequest { BucketName = bucket, Key = strItemKey }; using (GetObjectResponse response = client.GetObject(request)) { response.WriteResponseStreamToFile(strDestination); } return strDestination; } public void copyItem(string strItemKeySource, string strItemKeyDestination) { CopyObjectRequest copyRequest = new CopyObjectRequest { SourceBucket = bucket, SourceKey = strItemKeySource, DestinationBucket = bucket, DestinationKey = strItemKeyDestination }; CopyObjectResponse copyResponse = client.CopyObject(copyRequest); if (copyResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) throw new Exception("Item has an error"); } public void deleteItem(string strItemKey) { DeleteObjectRequest deleteObject = new DeleteObjectRequest { BucketName = bucket, Key = strItemKey }; DeleteObjectResponse deleteResponse = client.DeleteObject(deleteObject); } }

Hope this helps to someone else.

更多推荐

本文发布于:2023-08-05 14:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   如何在   WinForm   AWS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!