C# 如何在控制台应用程序中使用c将文件从本地计算机上载到s3 glacier的vault?
有人知道怎么做吗,因为我已经调查过了,但我只发现了错误/不起作用的答案我尝试了很多解决方案,但似乎都是错误的,比如使用Chilkat目录,使用ArchiveTransferManagerC# 如何在控制台应用程序中使用c将文件从本地计算机上载到s3 glacier的vault?,c#,amazon-web-services,aws-sdk,amazon-glacier,aws-sdk-net,C#,Amazon Web Services,Aws Sdk,Amazon Glacier,Aws Sdk Net,有人知道怎么做吗,因为我已经调查过了,但我只发现了错误/不起作用的答案我尝试了很多解决方案,但似乎都是错误的,比如使用Chilkat目录,使用ArchiveTransferManager Chilkat.Rest rest = new Chilkat.Rest(); bool bTls = true; int port = 443; bool bAutoReconnect = true; bool success
Chilkat.Rest rest = new Chilkat.Rest();
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
bool success = rest.Connect("glacier.eu-west-1.amazonaws.com", port, bTls, bAutoReconnect);
Chilkat.AuthAws authAws = new Chilkat.AuthAws();
authAws.AccessKey = ;
authAws.SecretKey = ;
authAws.ServiceName = "glacier";
authAws.Region = "us-west-1";
success = rest.SetAuthAws(authAws);
rest.AddHeader("x-amz-glacier-version", "2012-06-01");
string filePath = "20190422.csv";
Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
crypt.HashAlgorithm = "sha256-tree-hash";
crypt.EncodingMode = "hexlower";
string treeHashHex = crypt.HashFileENC(filePath);
rest.AddHeader("x-amz-sha256-tree-hash", treeHashHex);
crypt.HashAlgorithm = "sha256";
string linearHashHex = crypt.HashFileENC(filePath);
authAws.PrecomputedSha256 = linearHashHex;
rest.AddHeader("x-amz-archive-description", filePath);
Chilkat.Stream fileStream = new Chilkat.Stream();
fileStream.SourceFile = filePath;
string responseStr = rest.FullRequestStream("POST", "/682988997959/vaults/streamqueuesvault", fileStream);
if (rest.LastMethodSuccess != true)
{
Debug.WriteLine(rest.LastErrorText);
return;
}
int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400)
{
Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
Debug.WriteLine("Response Header:");
Debug.WriteLine(rest.ResponseHeader);
Debug.WriteLine("Response Body:");
Debug.WriteLine(responseStr);
return;
}
Debug.WriteLine("response status code = " + Convert.ToString(respStatusCode));
string archiveId = rest.ResponseHdrByName("x-amz-archive-id");
Debug.WriteLine("x-amz-archive-id = " + archiveId);
string location = rest.ResponseHdrByName("Location");
Debug.WriteLine("Location = " + location);
也许这会有帮助
AmazonS3Client S3Client = new AmazonS3Client (credentials,region);
// Create a client
AmazonS3Client client = new AmazonS3Client();
// Create a PutObject request
PutObjectRequest request = new PutObjectRequest
{
BucketName = "SampleBucket",
Key = "Item1",
FilePath = "contents.txt"
};
// Put object
PutObjectResponse response = client.PutObject(request);
Source=确保您所在的地区是一致的。在下面的代码中,在Connect调用中使用eu-west-1,但是us-west-1用于authAws.Region
bool success = rest.Connect("glacier.eu-west-1.amazonaws.com", port, bTls, bAutoReconnect);
Chilkat.AuthAws authAws = new Chilkat.AuthAws();
authAws.AccessKey = ;
authAws.SecretKey = ;
authAws.ServiceName = "glacier";
authAws.Region = "us-west-1";
下面是一个分步指南,介绍如何在控制台应用程序?中使用c将文件从我的本地机器上载到s3 glacier的vault?。首先,我想介绍一些基本的背景信息,这些信息将在以后的解决方案中使用。如果你对S3冰川很在行的话,可以直接跳到解决方案上来 如果已经安装了用于.NET和VS的AWS SDK,则可以 S3冰川简介 是亚马逊的低成本长期存储服务 在冰川术语中,对象称为档案。存储档案的文件夹也称为Vault。这很简单-从以下方面: 问:亚马逊S3冰川内的数据是如何组织的? 您将数据作为归档文件存储在Amazon S3冰川中。每个存档都分配了一个唯一的存档ID,以后可用于检索数据。归档文件可以表示单个文件,也可以选择将多个文件合并以作为单个归档文件上载。你可以将档案上传到保险库。Vault是用于组织数据的归档的集合 将对象上载到S3 Glacier时,对象不会立即显示在Glacier控制台中。您的冰川控制台将每天刷新一次 Amazon建议您在开发与AWS服务接口的C应用程序时使用AWS SDK for.NET 简单解 在编码之前,进入AWS控制台并创建S3 Glacier Vault名称“TestVault” 在2019年4月发布此解决方案时,我建议您使用Visual Studio 2019。这些步骤与早期版本的Visual Studio类似 我提供的代码是直接从 visual studio准备就绪后,请执行以下步骤: 创建一个新的项目使用模板->控制台应用程序.NET框架-而不是控制台应用程序.NET核心,并将其命名为ConsoleApp9 通过将AWS SDK添加到项目中。 工具菜单,选择Nuget Package Manager,然后单击Package Manager控制台。 然后键入安装包AWSSDK 对于MAC使用项目->添加Nuget软件包。搜索AWSSDK.Glacier并安装它 下面是工作代码。您需要将大部分代码复制到Program.cs中,并删除默认的Hello World代码。您的最终Program.cs代码应该如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Amazon.Glacier;
using Amazon.Glacier.Transfer;
using Amazon.Runtime;
namespace ConsoleApp9
{
class Program
{
static string vaultName = "TestVault";
static string archiveToUpload = "C:\\Windows\\Temp\\TEST-ARCHIVE.txt";
static void Main(string[] args)
{
try
{
var manager = new ArchiveTransferManager(Amazon.RegionEndpoint.USEast1);
// Upload an archive.
string archiveId = manager.Upload(vaultName, "upload archive test", archiveToUpload).ArchiveId;
Console.WriteLine("Archive ID: (Copy and save this ID for use in other examples.) : {0}", archiveId);
Console.WriteLine("To continue, press Enter");
Console.ReadKey();
}
catch (AmazonGlacierException e) { Console.WriteLine(e.Message); }
catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
catch (Exception e) { Console.WriteLine(e.Message); }
Console.WriteLine("To continue, press Enter");
Console.ReadKey();
}
}
}
将要上载到Glacier的文件作为c:\Windows\Temp\Test-Archive.txt。您可以将文件放在任何需要的地方,只需在代码中更新变量archiveToUpload以反映位置
如果您的区域不可用ast1,请在尝试后更改线上的AWS区域:
var manager=new ArchiveTransferManagerAmazon.RegionEndpoint.YOUR-REGION
运行程序,它将上载文件。如果您在此之前安装了AWS SDK,那么它可能工作正常,并且您将有一个屏幕显示您的存档id:
如果您遇到权限或授权错误-请。我建议使用顶部的第二个凭证文件选项。其他问题可能是Vault名称错误或在您的计算机上找不到文件。
当你回到Glacier控制台时,你不会看到上传的任何文件。与s3相比,Glacier成本低,移动速度慢,因此您的Vault内容每天更新一次。
只要您在步骤6中获得ID,您的文件就成功地存储在Glacier中
希望这有助于你找到成功 请不要在StackOverflow共享任何密钥/密码!特别是关于连接到哪里这样的信息。