我正在尝试构建另一个服务将用于将PDF上载到S3存储桶的dll。我尝试了各种解决方案,这些解决方案大多是从AWS文档中复制的,或者是其他答案,以下两者似乎都在测试中,但没有任何东西上载到s3存储桶。
尝试1:异步上传
using Amazon.S3;
using Amazon.S3.Transfer;
using System;
using System.Threading.Tasks;
namespace Amazon.DocSamples.S3
{
public class UploadFileMPUHighLevelAPITest
{
private const string bucketName = "some-demo";
private const string keyName = "some-key";
private const string filePath = @"C:\Users\ME\Documents\sample.pdf";
// Specify your bucket region (an example region is shown).
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.EUWest3;
private static IAmazonS3 s3Client;
public static void Main()
{
s3Client = new AmazonS3Client("*AccessKey*", "*SecretKey*", bucketRegion);
UploadFileAsync().Wait();
}
private static async Task UploadFileAsync()
{
try
{
var fileTransferUtility =
new TransferUtility(s3Client);
// Option 1. Upload a file. The file name is used as the object key name.
await fileTransferUtility.UploadAsync(filePath, bucketName);
Console.WriteLine("Upload 1 completed");
// Option 2. Specify object key name explicitly.
await fileTransferUtility.UploadAsync(filePath, bucketName, keyName);
Console.WriteLine("Upload 2 completed");
// Option 4. Specify advanced settings.
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
{
BucketName = bucketName,
FilePath = filePath,
StorageClass = S3StorageClass.StandardInfrequentAccess,
PartSize = 6291456, // 6 MB.
Key = keyName
};
fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
await fileTransferUtility.UploadAsync(fileTransferUtilityRequest);
Console.WriteLine("Upload 4 completed");
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
}
}
然后,我创建了一个控制台应用程序以运行以上命令:
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Amazon.DocSamples.S3.UploadFileMPUHighLevelAPITest.Main();
}
}
}
运行此命令的结果是打印了日志语句,并且应用程序没有抛出就退出了。但是我的水桶里什么都没有!
尝试2:同步方法
using System;
using System.Diagnostics;
using Amazon.S3;
using Amazon.S3.Model;
namespace AmazonS3Demo
{
public class AmazonS3Uploader
{
private string bucketName = "some-demo";
private string keyName = "the-name-of-your-file";
private string filePath = "C:\\Users\\ME\\Documents\\sample.pdf";
public void UploadFile(string AWSAccessKey, string AWSSecretKey)
{
var client = new AmazonS3Client(AWSAccessKey, AWSSecretKey, Amazon.RegionEndpoint.EUWest1);
try
{
PutObjectRequest putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
FilePath = filePath,
ContentType = "application/pdf"
};
PutObjectResponse response = client.PutObject(putRequest);
Debug.WriteLine(response.HttpStatusCode);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
throw new Exception("Check the provided AWS Credentials.");
}
else
{
throw new Exception("Error occurred: " + amazonS3Exception.Message);
}
}
}
}
}
同样,制作了一个控制台应用程序来对此进行测试:
using System;
using AmazonS3Demo;
namespace DemoExe
{
class Program
{
static void Main(string[] args)
{
try
{
AmazonS3Uploader s3 = new AmazonS3Uploader();
string AWSAccessKey = "*AccessKey*";
string AWSSecretKey = "*SecretKey";
s3.UploadFile(AWSAccessKey, AWSSecretKey);
}
catch (Exception e)
{
Console.WriteLine(
"Error encountered ***. Message:'{0}' when writing an object"
, e.Message);
}
}
}
}
与上次相同,该应用程序记录一条“ OK”消息,然后退出。但是我的S3存储桶中没有文件!
有人可以指出我正确的方向吗?