/*******************************************************************************
* Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace Amazon.PowerShell.Cmdlets.S3
{
///
/// Returns some or all (up to 1,000) of the objects in a bucket with each request. You
/// can use the request parameters as selection criteria to return a subset of the objects
/// in a bucket. A 200 OK
response can contain valid or invalid XML. Make
/// sure to design your application to parse the contents of the response and handle it
/// appropriately. Objects are returned sorted in an ascending order of the respective
/// key names in the list. For more information about listing objects, see Listing
/// object keys programmatically in the Amazon S3 User Guide.
///
///
///
/// To use this operation, you must have READ access to the bucket.
///
/// To use this action in an Identity and Access Management (IAM) policy, you must have
/// permission to perform the s3:ListBucket
action. The bucket owner has
/// this permission by default and can grant this permission to others. For more information
/// about permissions, see Permissions
/// Related to Bucket Subresource Operations and Managing
/// Access Permissions to Your Amazon S3 Resources in the Amazon S3 User Guide.
///
/// This section describes the latest revision of this action. We recommend that you use
/// this revised API operation for application development. For backward compatibility,
/// Amazon S3 continues to support the prior version of this API operation, ListObjects.
///
/// To get a list of your buckets, see ListBuckets.
///
/// The following operations are related to ListObjectsV2
:
///
This cmdlet automatically pages all available results to the pipeline - parameters related to iteration are only needed if you want to manually control the paginated output. To disable autopagination, use -NoAutoIteration.
///
[Cmdlet("Get", "S3ObjectV2", DefaultParameterSetName="GetMultipleObjects")]
[OutputType("Amazon.S3.Model.S3Object")]
[AWSCmdlet("Calls the Amazon Simple Storage Service (S3) ListObjectsV2 API operation.", Operation = new[] {"ListObjectsV2"}, SelectReturnType = typeof(Amazon.S3.Model.ListObjectsV2Response))]
[AWSCmdletOutput("Amazon.S3.Model.S3Object or Amazon.S3.Model.ListObjectsV2Response",
"This cmdlet returns a collection of Amazon.S3.Model.S3Object objects.",
"The service call response (type Amazon.S3.Model.ListObjectsV2Response) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class GetS3ObjectV2Cmdlet : AmazonS3ClientCmdlet, IExecutor
{
#region Parameter BucketName
///
///
/// Bucket name to list. When using this action with an access point, you must direct requests to the access
/// point hostname. The access point hostname takes the form AccessPointName-AccountId.s3-accesspoint.Region.amazonaws.com.
/// When using this action with an access point through the Amazon Web Services SDKs,
/// you provide the access point ARN in place of the bucket name. For more information
/// about access point ARNs, see Using
/// access points in the Amazon S3 User Guide.When you use this action with Amazon S3 on Outposts, you must direct requests to the
/// S3 on Outposts hostname. The S3 on Outposts hostname takes the form AccessPointName-AccountId.outpostID.s3-outposts.Region.amazonaws.com
.
/// When you use this action with S3 on Outposts through the Amazon Web Services SDKs,
/// you provide the Outposts access point ARN in place of the bucket name. For more information
/// about S3 on Outposts ARNs, see What
/// is S3 on Outposts? in the Amazon S3 User Guide.
///
///
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)]
public System.String BucketName { get; set; }
#endregion
#region Parameter Encoding
///
///
/// Requests Amazon S3 to encode the object keys in the response and specifies
/// the encoding method to use. An object key may contain any Unicode character;
/// however, XML 1.0 parser cannot parse some characters, such as characters
/// with an ASCII value from 0 to 10. For characters that are not supported in
/// XML 1.0, you can add this parameter to request that Amazon S3 encode the
/// keys in the response.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.S3.EncodingType")]
public Amazon.S3.EncodingType Encoding { get; set; }
#endregion
#region Parameter ExpectedBucketOwner
///
///
/// The account ID of the expected bucket owner. If the bucket is owned by a different
/// account, the request will fail with an HTTP 403 (Access Denied)
error.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ExpectedBucketOwner { get; set; }
#endregion
#region Parameter FetchOwner
///
///
/// The owner field is not present in ListObjectsV2
by default. If you want
/// to return the owner field with each key in the result, then set the FetchOwner
/// field to true
..
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? FetchOwner { get; set; }
#endregion
#region Parameter OptionalObjectAttribute
///
///
/// Specifies the optional fields that you want returned in the response. Fields that
/// you do not specify are not returned.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("OptionalObjectAttributes")]
public System.String[] OptionalObjectAttribute { get; set; }
#endregion
#region Parameter RequestPayer
///
///
/// Confirms that the requester knows that she or he will be charged for the list objects
/// request in V2 style. Bucket owners need not specify this parameter in their requests.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.S3.RequestPayer")]
public Amazon.S3.RequestPayer RequestPayer { get; set; }
#endregion
#region Parameter StartAfter
///
///
/// StartAfter is where you want Amazon S3 to start listing from. Amazon S3 starts listing
/// after this specified key. StartAfter can be any key in the bucket.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String StartAfter { get; set; }
#endregion
#region Parameter ContinuationToken
///
///
/// ContinuationToken
indicates to Amazon S3 that the list is being continued
/// on this bucket with a token. ContinuationToken
is obfuscated and is not
/// a real key.
///
///
///
Note: This parameter is only used if you are manually controlling output pagination of the service API call.
///
In order to manually control output pagination, use '-ContinuationToken $null' for the first call and '-ContinuationToken $AWSHistory.LastServiceResponse.NextContinuationToken' for subsequent calls.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "GetMultipleObjects")]
[Alias("NextToken")]
public System.String ContinuationToken { get; set; }
#endregion
#region Parameter Delimiter
///
///
/// A delimiter is a character that you use to group keys.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "GetMultipleObjects")]
public System.String Delimiter { get; set; }
#endregion
#region Parameter MaxKey
///
///
/// Sets the maximum number of keys returned in the response. By default the action returns
/// up to 1,000 key names. The response might contain fewer keys but will never contain
/// more.
///
///
///
Note: In AWSPowerShell and AWSPowerShell.NetCore this parameter is used to limit the total number of items returned by the cmdlet.
///
In AWS.Tools this parameter is simply passed to the service to specify how many items should be returned by each service call.
///
Pipe the output of this cmdlet into Select-Object -First to terminate retrieving data pages early and control the number of items returned.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "GetMultipleObjects")]
[Alias("MaxItems","MaxKeys")]
public int? MaxKey { get; set; }
#endregion
#region Parameter Prefix
///
///
/// Limits the response to keys that begin with the specified prefix.
///
///
[System.Management.Automation.Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "GetMultipleObjects")]
public System.String Prefix { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'S3Objects'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.S3.Model.ListObjectsV2Response).
/// Specifying the name of a property of type Amazon.S3.Model.ListObjectsV2Response will result in that property being returned.
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public string Select { get; set; } = "S3Objects";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the BucketName parameter.
/// The -PassThru parameter is deprecated, use -Select '^BucketName' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^BucketName' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
#region Parameter NoAutoIteration
///
/// By default the cmdlet will auto-iterate and retrieve all results to the pipeline by performing multiple
/// service calls. If set, the cmdlet will retrieve only the next 'page' of results using the value of ContinuationToken
/// as the start point.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter NoAutoIteration { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "s3";
base.ProcessRecord();
var context = new CmdletContext();
// allow for manipulation of parameters prior to loading into context
PreExecutionContextLoad(context);
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
if (this.PassThru.IsPresent)
{
throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select));
}
}
else if (this.PassThru.IsPresent)
{
context.Select = (response, cmdlet) => this.BucketName;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.BucketName = this.BucketName;
context.ContinuationToken = this.ContinuationToken;
context.Delimiter = this.Delimiter;
context.Encoding = this.Encoding;
context.ExpectedBucketOwner = this.ExpectedBucketOwner;
context.FetchOwner = this.FetchOwner;
context.MaxKey = this.MaxKey;
#if !MODULAR
if (ParameterWasBound(nameof(this.MaxKey)) && this.MaxKey.HasValue)
{
WriteWarning("AWSPowerShell and AWSPowerShell.NetCore use the MaxKey parameter to limit the total number of items returned by the cmdlet." +
" This behavior is obsolete and will be removed in a future version of these modules. Pipe the output of this cmdlet into Select-Object -First to terminate" +
" retrieving data pages early and control the number of items returned. AWS.Tools already implements the new behavior of simply passing MaxKey" +
" to the service to specify how many items should be returned by each service call.");
}
#endif
if (this.OptionalObjectAttribute != null)
{
context.OptionalObjectAttribute = new List(this.OptionalObjectAttribute);
}
context.Prefix = this.Prefix;
context.RequestPayer = this.RequestPayer;
context.StartAfter = this.StartAfter;
// allow further manipulation of loaded context prior to processing
PostExecutionContextLoad(context);
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
#if MODULAR
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
var useParameterSelect = this.Select.StartsWith("^") || this.PassThru.IsPresent;
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
// create request and set iteration invariants
var request = new Amazon.S3.Model.ListObjectsV2Request();
if (cmdletContext.BucketName != null)
{
request.BucketName = cmdletContext.BucketName;
}
if (cmdletContext.Delimiter != null)
{
request.Delimiter = cmdletContext.Delimiter;
}
if (cmdletContext.Encoding != null)
{
request.Encoding = cmdletContext.Encoding;
}
if (cmdletContext.ExpectedBucketOwner != null)
{
request.ExpectedBucketOwner = cmdletContext.ExpectedBucketOwner;
}
if (cmdletContext.FetchOwner != null)
{
request.FetchOwner = cmdletContext.FetchOwner.Value;
}
if (cmdletContext.MaxKey != null)
{
request.MaxKeys = AutoIterationHelpers.ConvertEmitLimitToServiceTypeInt32(cmdletContext.MaxKey.Value);
}
if (cmdletContext.OptionalObjectAttribute != null)
{
request.OptionalObjectAttributes = cmdletContext.OptionalObjectAttribute;
}
if (cmdletContext.Prefix != null)
{
request.Prefix = cmdletContext.Prefix;
}
if (cmdletContext.RequestPayer != null)
{
request.RequestPayer = cmdletContext.RequestPayer;
}
if (cmdletContext.StartAfter != null)
{
request.StartAfter = cmdletContext.StartAfter;
}
// Initialize loop variant and commence piping
var _nextToken = cmdletContext.ContinuationToken;
var _userControllingPaging = this.NoAutoIteration.IsPresent || ParameterWasBound(nameof(this.ContinuationToken));
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
do
{
request.ContinuationToken = _nextToken;
CmdletOutput output;
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
if (!useParameterSelect)
{
pipelineOutput = cmdletContext.Select(response, this);
}
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
_nextToken = response.NextContinuationToken;
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
}
ProcessOutput(output);
} while (!_userControllingPaging && AutoIterationHelpers.HasValue(_nextToken));
if (useParameterSelect)
{
WriteObject(cmdletContext.Select(null, this));
}
return null;
}
#else
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
var useParameterSelect = this.Select.StartsWith("^") || this.PassThru.IsPresent;
// create request and set iteration invariants
var request = new Amazon.S3.Model.ListObjectsV2Request();
if (cmdletContext.BucketName != null)
{
request.BucketName = cmdletContext.BucketName;
}
if (cmdletContext.Delimiter != null)
{
request.Delimiter = cmdletContext.Delimiter;
}
if (cmdletContext.Encoding != null)
{
request.Encoding = cmdletContext.Encoding;
}
if (cmdletContext.ExpectedBucketOwner != null)
{
request.ExpectedBucketOwner = cmdletContext.ExpectedBucketOwner;
}
if (cmdletContext.FetchOwner != null)
{
request.FetchOwner = cmdletContext.FetchOwner.Value;
}
if (cmdletContext.OptionalObjectAttribute != null)
{
request.OptionalObjectAttributes = cmdletContext.OptionalObjectAttribute;
}
if (cmdletContext.Prefix != null)
{
request.Prefix = cmdletContext.Prefix;
}
if (cmdletContext.RequestPayer != null)
{
request.RequestPayer = cmdletContext.RequestPayer;
}
if (cmdletContext.StartAfter != null)
{
request.StartAfter = cmdletContext.StartAfter;
}
// Initialize loop variants and commence piping
System.String _nextToken = null;
int? _emitLimit = null;
int _retrievedSoFar = 0;
if (AutoIterationHelpers.HasValue(cmdletContext.ContinuationToken))
{
_nextToken = cmdletContext.ContinuationToken;
}
if (cmdletContext.MaxKey.HasValue)
{
// The service has a maximum page size of 1000. If the user has
// asked for more items than page max, and there is no page size
// configured, we rely on the service ignoring the set maximum
// and giving us 1000 items back. If a page size is set, that will
// be used to configure the pagination.
// We'll make further calls to satisfy the user's request.
_emitLimit = cmdletContext.MaxKey;
}
var _userControllingPaging = this.NoAutoIteration.IsPresent || ParameterWasBound(nameof(this.ContinuationToken));
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
do
{
request.ContinuationToken = _nextToken;
if (_emitLimit.HasValue)
{
int correctPageSize = Math.Min(1000, _emitLimit.Value);
request.MaxKeys = AutoIterationHelpers.ConvertEmitLimitToInt32(correctPageSize);
}
CmdletOutput output;
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
if (!useParameterSelect)
{
pipelineOutput = cmdletContext.Select(response, this);
}
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
int _receivedThisCall = response.S3Objects.Count;
_nextToken = response.NextContinuationToken;
_retrievedSoFar += _receivedThisCall;
if (_emitLimit.HasValue)
{
_emitLimit -= _receivedThisCall;
}
}
catch (Exception e)
{
if (_retrievedSoFar == 0 || !_emitLimit.HasValue)
{
output = new CmdletOutput { ErrorResponse = e };
}
else
{
break;
}
}
ProcessOutput(output);
} while (!_userControllingPaging && AutoIterationHelpers.HasValue(_nextToken) && (!_emitLimit.HasValue || _emitLimit.Value >= 1));
if (useParameterSelect)
{
WriteObject(cmdletContext.Select(null, this));
}
return null;
}
#endif
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.S3.Model.ListObjectsV2Response CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.ListObjectsV2Request request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Simple Storage Service (S3)", "ListObjectsV2");
try
{
#if DESKTOP
return client.ListObjectsV2(request);
#elif CORECLR
return client.ListObjectsV2Async(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal partial class CmdletContext : ExecutorContext
{
public System.String BucketName { get; set; }
public System.String ContinuationToken { get; set; }
public System.String Delimiter { get; set; }
public Amazon.S3.EncodingType Encoding { get; set; }
public System.String ExpectedBucketOwner { get; set; }
public System.Boolean? FetchOwner { get; set; }
public int? MaxKey { get; set; }
public List OptionalObjectAttribute { get; set; }
public System.String Prefix { get; set; }
public Amazon.S3.RequestPayer RequestPayer { get; set; }
public System.String StartAfter { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.S3Objects;
}
}
}