【问题标题】:Upload file to S3 bucket: a null reference exception on await fileTransferUtility.UploadAsync(filePath, _bucket)将文件上传到 S3 存储桶:等待 fileTransferUtility.UploadAsync(filePath, _bucket) 出现空引用异常
【发布时间】:2020-12-15 15:14:01
【问题描述】:

我正在尝试将文件上传到 S3 存储桶,但出现空引用异常 await fileTransferUtility.UploadAsync(filePath, _bucket)

public class FileUploadService : IFileUploadService
{
    private readonly string _bucket;

    public FileUploadService()
    {
        _bucket = ConfigurationManager.AppSettings["S3Bucket"];
    }

    public async Task UploadFile(string filePath, CognitoAWSCredentials cred)
    {
        IAmazonS3 client = new AmazonS3Client(cred);
        TransferUtility fileTransferUtility = new TransferUtility(client);
        await fileTransferUtility.UploadAsync(filePath, _bucket);
    }
}

这是异常堆栈跟踪的相关部分:

在 Amazon.S3.Util.BucketRegionDetector.GetUsEast1ClientFromCredentials(ImmutableCredentials 凭据)在 Amazon.S3.Util.BucketRegionDetector.GetHeadBucketPreSignedUrl(字符串 bucketName,ImmutableCredentials 凭证)在 Amazon.S3.Util.BucketRegionDetector.d__13.MoveNext() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Amazon.S3.Util.BucketRegionDetector.d__12.MoveNext() ...

不知道我做错了什么,因为我是 AWS 的新手。我通过以下方式检索CognitoAWSCredentials

string clientId = ConfigurationManager.AppSettings["CLIENT_ID"];
string poolId = ConfigurationManager.AppSettings["USERPOOL_ID"];
RegionEndpoint region = RegionEndpoint.EnumerableAllRegions.First(q => q.SystemName == "us-east-1");
provider = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), region);    

CognitoUserPool userPool = new CognitoUserPool(poolId, clientId, provider);
CognitoUser user = new CognitoUser(username, clientId, userPool, provider);

AuthFlowResponse context = await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
    Password = password
});

CognitoAWSCredentials cred = user.GetCognitoAWSCredentials(poolId, region);

【问题讨论】:

    标签: c# amazon-web-services amazon-s3 amazon-cognito


    【解决方案1】:

    您的凭据对于 UploadAsync 调用无效。

    (请参阅:https://github.com/aws/aws-sdk-net/issues/1166,了解为什么会出现空引用异常。)

    您应该使用身份池 ID 而不是用户池 ID 调用 GetCognitoAWSCredentials(poolId, region)。请参阅此处的“身份验证后使用 AWS 资源”部分:https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/cognito-authentication-extension.html

    为避免空引用异常,请在使用 CognitoAWSCredentials 进行任何 S3 调用之前调用 GetCredentialsAsync。比如:

    public async Task UploadFile(string filePath, CognitoAWSCredentials cred)
    {
        try
        {
            // AmazonS3Client behaves badly if its GetCredentialsAsync() call throws certain exceptions,
            // so try for ourselves first.  We are doomed anyway if we can't get proper credentials.
            _ = await cred.GetCredentialsAsync();
        }
        catch (Exception e)
        {
            Trace.WriteLine($"Exception getting credentials: {e.Message}");
            throw;
        }
    
        IAmazonS3 client = new AmazonS3Client(cred);
        TransferUtility fileTransferUtility = new TransferUtility(client);
        await fileTransferUtility.UploadAsync(filePath, _bucket);
    }
    

    这样,您可以自己处理来自 GetCredentialsAsync() 的异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 2018-07-20
      • 1970-01-01
      • 2018-04-25
      • 2015-05-05
      • 2018-03-25
      • 2018-02-07
      • 1970-01-01
      相关资源
      最近更新 更多