MST

星途 面试题库

面试题:C# 中使用 AWS/Azure SDK 处理云存储的常见操作

假设你正在使用 C# 结合 AWS S3 SDK 或 Azure Blob Storage SDK 开发云原生应用,要求实现上传本地文件到云存储以及从云存储下载文件到本地的功能,请写出核心代码片段,并简要说明可能遇到的异常及处理方式。
48.3万 热度难度
编程语言C#

知识考点

AI 面试

面试题答案

一键面试

使用 AWS S3 SDK

  1. 上传本地文件到 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);
        }
    }
}
  1. 从 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);
        }
    }
}
  1. 可能遇到的异常及处理
    • AmazonS3Exception
      • InvalidAccessKeyId 或 SignatureDoesNotMatch:检查 AWS 凭证是否正确。
      • 其他错误:根据异常消息进行排查,可能是权限问题、网络问题等。
    • System.Exception:捕获其他未处理的异常,打印异常信息用于调试。

使用 Azure Blob Storage SDK

  1. 上传本地文件到 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);
    }
}
  1. 从 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);
    }
}
  1. 可能遇到的异常及处理
    • RequestFailedException:包含 HTTP 状态码和错误码,可根据这些信息进行排查,如权限不足、资源不存在等。
    • System.Exception:捕获其他未处理的异常,打印异常信息用于调试。