2017-10-17 201 views
0

我正在使用C#和AWSSDK v3將文件上載到S3存儲桶。該文件使用ServerSideEncryptionCustomerMethod加密。我可以上傳文件,但是如果我使用S3FileInfo()檢查文件是否存在,存在的錯誤是(400)錯誤請求。但是,如果我在上傳例程中註釋了指定加密的行,則S3FileInfo()。Exists將找到該文件而不會引發錯誤。我做錯了什麼?還是有一種不同的方式來檢查加密時文件是否存在?Amazon.S3.IO.S3FileInfo()。存在對加密文件返回400錯誤請求

這是我上傳的程序:

 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; 
    } 

回答

0

嗯,我認爲類/方法我所用是一個不支持加密的高級API。我改變了我的代碼來做一個元數據查詢來查看是否有任何東西回來。如果找不到文件,它會在我檢查的s3Exception中拋出一個「NotFound」ErrorCode。希望這可以幫助別人。如果別人提出更好的方法,我也很想學習它。

 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; 
    }