Amazon S3 .NET SDK加密大于5GB的对象

时间:2014-02-28 20:20:41

标签: encryption amazon-web-services amazon-s3

我环顾四周并没有遇到任何特定于.NET的内容,所以现在我在这里寻求帮助。我需要上传和加密(在上传操作期间)大于5GB的对象。我还需要将这些对象从文件夹复制到存储桶中的文件夹中。我可以上传和复制没有问题,问题是对象的服务器端加密没有按预期发生。在这两种情况下,我都在尝试使用CopyPartRequest类来执行加密。我的代码如下。在此操作之后,对象在那里并且是正确的大小,只是没有加密。有什么我想念的吗?

Dim s3Client As IAmazonS3
s3Client = New AmazonS3Client(accessKey, secretAccessKeyID, Amazon.RegionEndpoint.USEast1)

Dim uploadResponses As New List(Of UploadPartResponse)()
Dim copyResponses As New List(Of CopyPartResponse)()

Dim initiateRequest As New InitiateMultipartUploadRequest() With { _
    .BucketName = bucketName, _
    .Key = destObject _
}

Dim initResponse As InitiateMultipartUploadResponse = s3Client.InitiateMultipartUpload(initiateRequest)

Dim uploadId As [String] = initResponse.UploadId

Dim metadataRequest As New GetObjectMetadataRequest() With { _
    .BucketName = bucketName, _
    .Key = objToCopy _
}

Dim metadataResponse As GetObjectMetadataResponse = s3Client.GetObjectMetadata(metadataRequest)
Dim objectSize As Long = metadataResponse.ContentLength
Dim partSize As Long = 5 * CLng(Math.Pow(2, 20))

Dim bytePosition As Long = 0
Dim i As Integer = 1

While bytePosition < objectSize
    Dim copyRequest As New CopyPartRequest() With { _
        .DestinationBucket = bucketName, _
        .DestinationKey = destObject, _
        .SourceBucket = bucketName, _
        .SourceKey = objToCopy, _
        .UploadId = uploadId, _
        .FirstByte = bytePosition, _
        .LastByte = If(bytePosition + partSize - 1 >= objectSize, objectSize - 1, bytePosition + partSize - 1), _
         .ServerSideEncryptionMethod = Amazon.S3.ServerSideEncryptionMethod.AES256, _
         .PartNumber = i _
     }

     copyResponses.Add(s3Client.CopyPart(copyRequest))

     bytePosition += partSize

     If (bytePosition * 100) / objectSize >= 99 Then
         bgCopyMoveFile.ReportProgress(99)
     Else
         bgCopyMoveFile.ReportProgress((bytePosition * 100) / objectSize)
     End If

     i += 1
End While

根据我的阅读,在copypartrequest中指定加密应该这样做,但它不适合我。

上面的代码是vb.net,但我可以使用VB或C#。如果需要更多信息,请告诉我。

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果有人碰到这个问题,问题在于启动分段上传请求......我必须在那里指定AES256加密,所以

Dim initiateRequest As New InitiateMultipartUploadRequest() With { _
    .BucketName = bucketName, _
    .Key = destObject _
}

变成了

Dim initiateRequest As New InitiateMultipartUploadRequest() With { _
    .BucketName = bucketName, _
    .ServerSideEncryptionMethod = Amazon.S3.ServerSideEncryptionMethod.AES256, _  
    .Key = destObject _
}