使用 AWS S3 SDK
- 上传本地文件到 AWS S3
using Amazon.S3;
using Amazon.S3.Model;
using System.IO;
public async Task UploadFileToS3(string filePath, string bucketName, string objectKey)
{
using (var client = new AmazonS3Client())
{
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = objectKey,
FilePath = filePath
};
try
{
await client.PutObjectAsync(request);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
amazonS3Exception.ErrorCode.Equals("SignatureDoesNotMatch")))
{
Console.WriteLine("Check the provided AWS Credentials.");
Console.WriteLine("Error Message: {0}", amazonS3Exception.Message);
}
else
{
Console.WriteLine("Error Message: {0}", amazonS3Exception.Message);
}
}
catch (System.Exception e)
{
Console.WriteLine("Error Message: {0}", e.Message);
}
}
}
- 从 AWS S3 下载文件到本地
public async Task DownloadFileFromS3(string filePath, string bucketName, string objectKey)
{
using (var client = new AmazonS3Client())
{
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = objectKey
};
try
{
using (var response = await client.GetObjectAsync(request))
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await response.ResponseStream.CopyToAsync(fileStream);
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
amazonS3Exception.ErrorCode.Equals("SignatureDoesNotMatch")))
{
Console.WriteLine("Check the provided AWS Credentials.");
Console.WriteLine("Error Message: {0}", amazonS3Exception.Message);
}
else
{
Console.WriteLine("Error Message: {0}", amazonS3Exception.Message);
}
}
catch (System.Exception e)
{
Console.WriteLine("Error Message: {0}", e.Message);
}
}
}
- 可能遇到的异常及处理
- AmazonS3Exception:
- InvalidAccessKeyId 或 SignatureDoesNotMatch:检查 AWS 凭证是否正确。
- 其他错误:根据异常消息进行排查,可能是权限问题、网络问题等。
- System.Exception:捕获其他未处理的异常,打印异常信息用于调试。
使用 Azure Blob Storage SDK
- 上传本地文件到 Azure Blob Storage
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.IO;
public async Task UploadFileToAzureBlob(string filePath, string connectionString, string containerName, string blobName)
{
var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync();
var blobClient = containerClient.GetBlobClient(blobName);
try
{
await blobClient.UploadAsync(filePath, true);
}
catch (RequestFailedException requestFailedException)
{
Console.WriteLine($"HTTP status code {requestFailedException.Status}: {requestFailedException.ErrorCode}");
Console.WriteLine(requestFailedException.Message);
}
catch (System.Exception e)
{
Console.WriteLine("Error Message: {0}", e.Message);
}
}
- 从 Azure Blob Storage 下载文件到本地
public async Task DownloadFileFromAzureBlob(string filePath, string connectionString, string containerName, string blobName)
{
var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(blobName);
try
{
using (var fileStream = File.OpenWrite(filePath))
{
await blobClient.DownloadToAsync(fileStream);
}
}
catch (RequestFailedException requestFailedException)
{
Console.WriteLine($"HTTP status code {requestFailedException.Status}: {requestFailedException.ErrorCode}");
Console.WriteLine(requestFailedException.Message);
}
catch (System.Exception e)
{
Console.WriteLine("Error Message: {0}", e.Message);
}
}
- 可能遇到的异常及处理
- RequestFailedException:包含 HTTP 状态码和错误码,可根据这些信息进行排查,如权限不足、资源不存在等。
- System.Exception:捕获其他未处理的异常,打印异常信息用于调试。