You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS .NET C# SDK上传S3文件遇CS0122错误求助

解决AWS .NET SDK中AmazonS3Client.PutObject无法访问的问题

Hey there, that error (CS0122: 'AmazonS3Client.PutObject(PutObjectRequest)' is inaccessible due to its protection level) pops up because the synchronous PutObject method is no longer publicly available in AWS SDK for .NET v3—AWS has shifted to prioritizing async/await patterns for all API calls, so they made the old sync method internal (non-public).

Let's fix this step by step, plus correct another small issue in your original code:

修正后的异步代码(推荐方式)

First, make sure your containing method is marked async, then use the asynchronous PutObjectAsync method. Also, note that you were passing a file path to ContentBody—that property expects the actual text content of the file, not the path. Instead, use the FilePath property to let the SDK handle reading the file for you:

using Amazon.S3;
using Amazon.S3.Model;
using System.Threading.Tasks;

public async Task UploadFileToS3Async(string bucketName, string keyName, string filePath)
{
    var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2);
    try 
    {
        var putRequest = new PutObjectRequest 
        {
            BucketName = bucketName,
            Key = keyName,
            FilePath = filePath, // Correct way to specify the file to upload
            ContentType = "text/plain"
        };

        var response = await client.PutObjectAsync(putRequest);
        Console.WriteLine($"Upload succeeded! Status code: {response.HttpStatusCode}");
    }
    catch (AmazonS3Exception s3Ex)
    {
        Console.WriteLine($"S3-specific error: {s3Ex.Message}");
        Console.WriteLine($"AWS Error Code: {s3Ex.ErrorCode}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Unexpected error: {ex.Message}");
    }
}

关键原因说明

  1. Async-first SDK design: AWS SDK for .NET v3 deprecated public synchronous methods to push developers toward async patterns, which are better for scalability and responsiveness in applications. All public API methods now end with the Async suffix.
  2. ContentBody vs FilePath: Your original code used ContentBody = filePath, which would upload the string of the file path itself as the file content. Using FilePath tells the SDK to read the actual file from disk and upload it.

不推荐的同步替代方案(谨慎使用)

If you absolutely need to run this synchronously (e.g., in a legacy application), you can block on the async task with .Result or .Wait(). However, this can cause deadlocks (especially in UI or ASP.NET contexts), so it's not recommended:

var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2);
try 
{
    var putRequest = new PutObjectRequest 
    {
        BucketName = bucketName,
        Key = keyName,
        FilePath = filePath,
        ContentType = "text/plain"
    };

    var response = client.PutObjectAsync(putRequest).Result;
}
catch (Exception ex)
{
    // Handle exceptions here
}

内容的提问来源于stack exchange,提问作者Abhishek Pathak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:56:17