Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WinForm项目-CSharp中使用AWS S3_C#_Winforms_Amazon Web Services_Amazon S3 - Fatal编程技术网

C# 如何在WinForm项目-CSharp中使用AWS S3

C# 如何在WinForm项目-CSharp中使用AWS S3,c#,winforms,amazon-web-services,amazon-s3,C#,Winforms,Amazon Web Services,Amazon S3,我一直在读很多关于在VisualStudio中使用AWSS3的文章。但所有这些都集中在Web实现上 有人需要在WinForms-C中实现AWS S3吗?我在C中实现了一个类,它基本上与AWS S3交互,没有太多的配置 **我正在使用MS Framework 4.5** 首先,请记住使用NuGet软件包并安装AWS参考(在VS>>菜单>>工具>>NuGet软件包管理器>>管理解决方案的NuGet软件包管理器中): AWSSDK-亚马逊简单存储服务 AWSSDK-核心运行时 …实现了这个类,出于

我一直在读很多关于在VisualStudio中使用AWSS3的文章。但所有这些都集中在Web实现上

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

我在C中实现了一个类,它基本上与AWS S3交互,没有太多的配置

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

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

  • AWSSDK-亚马逊简单存储服务
  • AWSSDK-核心运行时
…实现了这个类,出于我的需要,我只需要访问密钥、密钥、区域和bucket(存在更多方法,但我只需要复制、读取和删除)

注意:不需要在app.config文件中进行额外配置,也不需要在用户文件夹中安装/创建配置文件

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);
    }
}
公共类clsAwsS3
{
字符串accessKey{get;set;}
字符串secretKey{get;set;}
字符串存储桶{get;set;}
RegionEndpoint区域{get;set;}
IAmazonS3客户;
公共clsAwsS3(字符串strBucket、字符串StrAccess Key、字符串strCretKey、RegionEndpoint region)
{
this.bucket=strBucket;
this.accessKey=strAccessKey;
this.secretKey=strSecretKey;
这个区域=区域;
登录();
}
私有void登录()
{
客户端=新AmazonS3Client(accessKey、secretKey、region);
}
公共列表getItems(字符串strPrefix=“”)
{
List lstResult=新列表();
ListObjectsV2请求listRequest;
如果(strPrefix==“”)
listRequest=新ListObjectsV2Request
{
BucketName=桶
};
其他的
listRequest=新ListObjectsV2Request
{
BucketName=bucket,
前缀=strPrefix
};
ListObjectsV2响应listResponse;
做
{
listResponse=client.ListObjectsV2(listRequest);
foreach(listResponse.S3Objects中的S3Object awsObject)
lstreult.Add(awsObject.Key);
listRequest.ContinuationToken=listResponse.NextContinuationToken;
}while(listResponse.IsTruncated);
返回结果;
}
公共字符串下载项(字符串strItemKey、字符串strDestination)
{
GetObjectRequest=新的GetObjectRequest
{
BucketName=bucket,
键=strItemKey
};
使用(GetObjectResponse=client.GetObject(请求))
{
response.WriterResponseStreamofile(strDestination);
}
返回目标;
}
public void copyItem(字符串strItemKeySource、字符串strItemKeyDestination)
{
CopyObjectRequest copyRequest=新建CopyObjectRequest
{
SourceBucket=bucket,
SourceKey=strItemKeySource,
DestinationBucket=桶,
DestinationKey=strItemKeyDestination
};
CopyObjectResponse copyResponse=client.CopyObject(copyRequest);
if(copyResponse.HttpStatusCode!=System.Net.HttpStatusCode.OK)
抛出新异常(“项有错误”);
}
public void deleteItem(string strItemKey)
{
DeleteObjectRequestDeleteObject=新的DeleteObjectRequest
{
BucketName=bucket,
键=strItemKey
};
DeleteObjectResponse deleteResponse=client.DeleteObject(DeleteObject);
}
}
希望这对其他人有所帮助。

我用C语言实现了一个类,它基本上与AWS S3交互,没有太多配置

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

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

  • AWSSDK-亚马逊简单存储服务
  • AWSSDK-核心运行时
…实现了这个类,出于我的需要,我只需要访问密钥、密钥、区域和bucket(存在更多方法,但我只需要复制、读取和删除)

注意:不需要在app.config文件中进行额外配置,也不需要在用户文件夹中安装/创建配置文件

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);
    }
}
公共类clsAwsS3
{
字符串accessKey{get;set;}
字符串secretKey{get;set;}
字符串存储桶{get;set;}
RegionEndpoint区域{get;set;}
IAmazonS3客户;
公共clsAwsS3(字符串strBucket、字符串StrAccess Key、字符串strCretKey、RegionEndpoint region)
{
this.bucket=strBucket;
this.accessKey=strAccessKey;
this.secretKey=strSecretKey;
这个区域=区域;
登录();
}
私有void登录()
{
客户端=新AmazonS3Client(accessKey、secretKey、region);
}
公共列表getItems(字符串strPrefix=“”)
{
List lstResult=新列表();
ListObjectsV2请求listRequest;
如果(strPrefix==“”)
listRequest=新ListObjectsV2Request
{
BucketName=桶
};
其他的
listRequest=新ListObjectsV2Request
{
BucketName=bucket,
前缀=strPrefix
};
ListObjectsV2响应listResponse;
做
{
listResponse=client.ListObjectsV2(listRequest);
foreach(listResponse.S3Objects中的S3Object awsObject)
lstreult.Add(awsObject.Key);
listRequest.ContinuationToken=listResponse.NextContinuationToken;
}while(listResponse.IsTruncated);
返回结果;
}
公共字符串下载项(字符串strItemKey、字符串strDestination)
{
GetObjectRequest=新的GetObjectRequest
{
巴克特纳