Amazon.S3.IO.S3FileInfo().存在返回 400 个加密文件的错误请求



我正在使用 C# 和 AWSSDK v3 将文件上传到 S3 存储桶中。 该文件使用 ServerSideEncryptionCustomerMethod 进行加密。 我可以上传文件,但是如果我使用 S3FileInfo() 检查该文件是否存在。存在,错误作为 (400) 错误请求引发。 但是,如果我注释掉在上传例程中指定加密的行,则 S3FileInfo()。存在 查找文件而不引发错误。 我做错了什么?或者是否有其他方法可以在加密文件时检查文件是否存在?

这是我的上传例程:

       public static string wfUpload(Stream pFileStream, string pBucketName, string pKeyName, string pCryptoKey) {
        string retVal = "";
        try {
            using (var lS3Client = new AmazonS3Client()) {
                Aes aesEncryption = Aes.Create();
                aesEncryption.KeySize = 256;
                aesEncryption.GenerateKey();
                string lCryptoKey = Convert.ToBase64String(aesEncryption.Key);
                PutObjectRequest request = new PutObjectRequest {
                    BucketName = pBucketName,
                    Key = pKeyName,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = lCryptoKey,
                };
                request.InputStream = pFileStream;
                PutObjectResponse response = lS3Client.PutObject(request);
                retVal = lCryptoKey;
            }
        }
        catch (AmazonS3Exception s3Exception) {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);
            throw (s3Exception);
        }
        catch (Exception e) {
            throw (e);
        }
        return retVal;
    }

以及我检查文件是否存在的例程:

       public static bool wfFileExists(String pBucketName, String pKeyName) {
        bool retVal = false;
        try {
            using (var lS3Client = new AmazonS3Client()) {
                if (new Amazon.S3.IO.S3FileInfo(lS3Client, pBucketName, pKeyName).Exists) {
                    retVal = true;
                }
            }
        }
        catch (AmazonS3Exception s3Exception) {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);
            throw (s3Exception);
        }
        catch (Exception e) {
            throw (e);
        }
        return retVal;
    }

好吧,我认为我使用的类/方法是不支持加密的高级 API 之一。 我更改了我的代码以执行元数据查询以查看是否有任何内容返回。 如果找不到文件,它会在我检查的 s3Exception 中抛出"NotFound"错误代码。希望这对其他人有所帮助。 如果其他人建议更好的方法,我也很乐意学习它。

        public static bool wfFileExists(String pBucketName, String pKeyName, String pCryptoKey) {
        bool retVal = false;
        try {
            using (var lS3Client = new AmazonS3Client()) {
                GetObjectMetadataRequest request = new GetObjectMetadataRequest {
                    BucketName = pBucketName,
                    Key = pKeyName,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = pCryptoKey,
                };
                GetObjectMetadataResponse lMetaData = lS3Client.GetObjectMetadata(request);
                // If an error is not thrown, we found the metadata.
                retVal = true;
            }
        }
        catch (AmazonS3Exception s3Exception) {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);
            if (s3Exception.ErrorCode != "NotFound") {
                throw (s3Exception);
            }
        }
        catch (Exception e) {
            throw (e);
        }
        return retVal;
    }

最新更新