Skip to content

Commit db23e57

Browse files
committed
Added ExpectedBucketOwner parameter to Write-S3Object and Copy-S3Object
1 parent 5c81adb commit db23e57

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

modules/AWSPowerShell/Cmdlets/S3/Advanced/Copy-S3Object-Cmdlet.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ public class CopyS3ObjectCmdlet : AmazonS3ClientCmdlet, IExecutor
236236
public SwitchParameter PublicReadWrite { get; set; }
237237
#endregion
238238

239+
#region Parameter ExpectedBucketOwner
240+
/// <summary>
241+
/// <para>
242+
/// <para>The account ID of the expected bucket owner. If the account ID that you provide does
243+
/// not match the actual owner of the bucket, the request fails with the HTTP status code
244+
/// <code>403 Forbidden</code> (access denied).</para>
245+
/// </para>
246+
/// </summary>
247+
[Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = CopyS3ObjectToS3Object)]
248+
public System.String ExpectedBucketOwner { get; set; }
249+
#endregion
250+
239251
#region Parameter StorageClass
240252

241253
// NOTE: This parameter does not use the marker attribute for automated validate set
@@ -636,6 +648,8 @@ protected override void ProcessRecord()
636648
break;
637649
}
638650

651+
if (this.ExpectedBucketOwner != null)
652+
context.ExpectedBucketOwner = this.ExpectedBucketOwner;
639653
if (ParameterWasBound("UtcModifiedSinceDate"))
640654
context.UtcModifiedSinceDate = this.UtcModifiedSinceDate;
641655
if (ParameterWasBound("UtcUnmodifiedSinceDate"))
@@ -731,7 +745,6 @@ private GetObjectMetadataResponse GetSourceObjectData(CmdletContext cmdletContex
731745
{
732746
request.RequestPayer = cmdletContext.RequestPayer;
733747
}
734-
735748
base.UserAgentAddition = AmazonS3Helper.GetCleanKeyUserAgentAdditionString(objectKey, request.Key);
736749

737750
var response = CallAWSServiceOperation(sourceRegionClient, request);
@@ -853,6 +866,8 @@ private object CopyS3ObjectToS3(ExecutorContext context)
853866
{
854867
request.RequestPayer = cmdletContext.RequestPayer;
855868
}
869+
if (cmdletContext.ExpectedBucketOwner != null)
870+
request.ExpectedBucketOwner = cmdletContext.ExpectedBucketOwner;
856871

857872
AmazonS3Helper.SetMetadataAndHeaders(request, cmdletContext.Metadata, cmdletContext.Headers);
858873

@@ -951,6 +966,8 @@ private object MultipartCopyS3ObjectToS3(ExecutorContext context, long objectSiz
951966
{
952967
completeRequest.RequestPayer = cmdletContext.RequestPayer;
953968
}
969+
if (cmdletContext.ExpectedBucketOwner != null)
970+
initiateRequest.ExpectedBucketOwner = cmdletContext.ExpectedBucketOwner;
954971

955972
CallAWSServiceOperation(Client, completeRequest);
956973
uploadId = null;
@@ -1320,6 +1337,7 @@ internal class CmdletContext : ExecutorContext
13201337
public S3MetadataDirective? MetadataDirective { get; set; }
13211338
public S3CannedACL CannedACL { get; set; }
13221339
public String SourceVersionId { get; set; }
1340+
public String ExpectedBucketOwner {get; set; }
13231341
public S3StorageClass StorageClass { get; set; }
13241342
public ServerSideEncryptionMethod ServerSideEncryptionMethod { get; set; }
13251343
public string ServerSideEncryptionKeyManagementServiceKeyId { get; set; }

modules/AWSPowerShell/Cmdlets/S3/Advanced/Write-S3Object-Cmdlet.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ public class WriteS3ObjectCmdlet : AmazonS3ClientCmdlet, IExecutor
246246

247247
#endregion
248248

249+
#region Parameter ExpectedBucketOwner
250+
/// <summary>
251+
/// <para>
252+
/// <para>The account ID of the expected bucket owner. If the account ID that you provide does
253+
/// not match the actual owner of the bucket, the request fails with the HTTP status code
254+
/// <code>403 Forbidden</code> (access denied).</para>
255+
/// </para>
256+
/// </summary>
257+
[Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = ParamSet_FromContent)]
258+
public System.String ExpectedBucketOwner { get; set; }
259+
#endregion
260+
249261
#region Shared Params
250262

251263
#region Parameter ContentType
@@ -566,6 +578,9 @@ protected override void ProcessRecord()
566578
else if (this.PublicReadWrite.IsPresent)
567579
context.CannedACL = S3CannedACL.PublicReadWrite;
568580

581+
if (this.ExpectedBucketOwner != null)
582+
context.ExpectedBucketOwner = this.ExpectedBucketOwner;
583+
569584
context.ContentType = this.ContentType;
570585

571586
if (ParameterWasBound("StorageClass"))
@@ -699,6 +714,9 @@ CmdletOutput UploadTextToS3(ExecutorContext context)
699714
request.ChecksumAlgorithm = cmdletContext.ChecksumAlgorithm;
700715
if (!string.IsNullOrEmpty(cmdletContext.IfNoneMatch))
701716
request.IfNoneMatch = cmdletContext.IfNoneMatch;
717+
if (cmdletContext.ExpectedBucketOwner != null)
718+
request.ExpectedBucketOwner = cmdletContext.ExpectedBucketOwner;
719+
702720

703721
#pragma warning disable CS0618 // A class member was marked with the Obsolete attribute
704722
request.CalculateContentMD5Header = cmdletContext.CalculateContentMD5Header;
@@ -886,6 +904,7 @@ private CmdletOutput UploadFolderToS3(ExecutorContext context)
886904
{
887905
request.RequestPayer = cmdletContext.RequestPayer;
888906
}
907+
889908

890909
AmazonS3Helper.SetExtraRequestFields(request, cmdletContext);
891910

@@ -964,6 +983,7 @@ internal class CmdletContext : ExecutorContext
964983
public String ChecksumValue { get; set; }
965984
public long? MpuObjectSize { get; set; }
966985

986+
public string ExpectedBucketOwner { get; set;}
967987
public Hashtable Metadata { get; set; }
968988
public Hashtable Headers { get; set; }
969989

tests/S3/S3.Tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ Describe -Tag "Smoke" "S3" {
6666
Write-S3Object -BucketName $script:bucketName -Key foo.txt -Content "this is a test" -ServerSideEncryption Naan
6767
} | Should -Throw
6868
}
69+
70+
It "Can verify bucket ownership during write operations" {
71+
$accountId = (Get-S3Bucket -BucketName $script:bucketName).Owner.ID
72+
Write-S3Object -BucketName $script:bucketName -Key "ownership-test.txt" -ExpectedBucketOwner $accountId -Content "testing bucket ownership verification"
73+
}
6974
}
7075

7176
Context "Reading" {
@@ -197,6 +202,15 @@ Describe -Tag "Smoke" "S3" {
197202
($tagCollection[0].Key) | Should -Be "testtag"
198203
($tagCollection[0].Value) | Should -Be "testvalue"
199204
}
205+
It "Can copy with ExpectedBucketOwner parameter" {
206+
$accountId = (Get-S3Bucket -BucketName $script:bucketName).Owner.ID
207+
Copy-S3Object -BucketName $eastBucketName -Key key -DestinationBucket $westBucketName -DestinationKey "key-copy-owner" -Region us-east-1 -ExpectedBucketOwner $accountId
208+
Read-S3Object -BucketName $westBucketName -Key "key-copy-owner" -File "temp\owner-copy.txt"
209+
(Get-Content "temp\owner-copy.txt") | Should -Be $content
210+
211+
$incorrectAccountId = "000000000000"
212+
{ Copy-S3Object -BucketName $eastBucketName -Key key -DestinationBucket $westBucketName -DestinationKey "key-copy-owner-fail" -Region us-east-1 -ExpectedBucketOwner $incorrectAccountId } | Should -Throw
213+
}
200214
}
201215

202216
Context "Checksums" {

0 commit comments

Comments
 (0)