Verwenden Sie ListObjectsV2 mit einem AWS SDKoder CLI - Amazon Simple Storage Service

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Verwenden Sie ListObjectsV2 mit einem AWS SDKoder CLI

Die folgenden Codebeispiele zeigen, wie man es benutztListObjectsV2.

Aktionsbeispiele sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Sie können diese Aktion in den folgenden Codebeispielen im Kontext sehen:

.NET
AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

/// <summary> /// Shows how to list the objects in an Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <param name="bucketName">The name of the bucket for which to list /// the contents.</param> /// <returns>A boolean value indicating the success or failure of the /// copy operation.</returns> public static async Task<bool> ListBucketContentsAsync(IAmazonS3 client, string bucketName) { try { var request = new ListObjectsV2Request { BucketName = bucketName, MaxKeys = 5, }; Console.WriteLine("--------------------------------------"); Console.WriteLine($"Listing the contents of {bucketName}:"); Console.WriteLine("--------------------------------------"); ListObjectsV2Response response; do { response = await client.ListObjectsV2Async(request); response.S3Objects .ForEach(obj => Console.WriteLine($"{obj.Key,-35}{obj.LastModified.ToShortDateString(),10}{obj.Size,10}")); // If the response is truncated, set the request ContinuationToken // from the NextContinuationToken property of the response. request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' getting list of objects."); return false; } }

Listen Sie Objekte mit einem Paginator auf.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// The following example lists objects in an Amazon Simple Storage /// Service (Amazon S3) bucket. /// </summary> public class ListObjectsPaginator { private const string BucketName = "amzn-s3-demo-bucket"; public static async Task Main() { IAmazonS3 s3Client = new AmazonS3Client(); Console.WriteLine($"Listing the objects contained in {BucketName}:\n"); await ListingObjectsAsync(s3Client, BucketName); } /// <summary> /// This method uses a paginator to retrieve the list of objects in an /// an Amazon S3 bucket. /// </summary> /// <param name="client">An Amazon S3 client object.</param> /// <param name="bucketName">The name of the S3 bucket whose objects /// you want to list.</param> public static async Task ListingObjectsAsync(IAmazonS3 client, string bucketName) { var listObjectsV2Paginator = client.Paginators.ListObjectsV2(new ListObjectsV2Request { BucketName = bucketName, }); await foreach (var response in listObjectsV2Paginator.Responses) { Console.WriteLine($"HttpStatusCode: {response.HttpStatusCode}"); Console.WriteLine($"Number of Keys: {response.KeyCount}"); foreach (var entry in response.S3Objects) { Console.WriteLine($"Key = {entry.Key} Size = {entry.Size}"); } } } }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for .NET APIReferenz.

Bash
AWS CLI mit Bash-Skript
Anmerkung

Es gibt noch mehr dazu. GitHub Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################### # function list_items_in_bucket # # This function displays a list of the files in the bucket with each file's # size. The function uses the --query parameter to retrieve only the key and # size fields from the Contents collection. # # Parameters: # $1 - The name of the bucket. # # Returns: # The list of files in text format. # And: # 0 - If successful. # 1 - If it fails. ############################################################################### function list_items_in_bucket() { local bucket_name=$1 local response response=$(aws s3api list-objects \ --bucket "$bucket_name" \ --output text \ --query 'Contents[].{Key: Key, Size: Size}') # shellcheck disable=SC2181 if [[ ${?} -eq 0 ]]; then echo "$response" else errecho "ERROR: AWS reports s3api list-objects operation failed.\n$response" return 1 fi }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS CLI Befehlsreferenz.

C++
SDKfür C++
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

bool AwsDoc::S3::listObjects(const Aws::String &bucketName, Aws::Vector<Aws::String> &keysResult, const Aws::S3::S3ClientConfiguration &clientConfig) { Aws::S3::S3Client s3Client(clientConfig); Aws::S3::Model::ListObjectsV2Request request; request.WithBucket(bucketName); Aws::String continuationToken; // Used for pagination. Aws::Vector<Aws::S3::Model::Object> allObjects; do { if (!continuationToken.empty()) { request.SetContinuationToken(continuationToken); } auto outcome = s3Client.ListObjectsV2(request); if (!outcome.IsSuccess()) { std::cerr << "Error: listObjects: " << outcome.GetError().GetMessage() << std::endl; return false; } else { Aws::Vector<Aws::S3::Model::Object> objects = outcome.GetResult().GetContents(); allObjects.insert(allObjects.end(), objects.begin(), objects.end()); continuationToken = outcome.GetResult().GetNextContinuationToken(); } } while (!continuationToken.empty()); std::cout << allObjects.size() << " object(s) found:" << std::endl; for (const auto &object: allObjects) { std::cout << " " << object.GetKey() << std::endl; keysResult.push_back(object.GetKey()); } return true; }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for C++ APIReferenz.

CLI
AWS CLI

Um eine Liste von Objekten in einem Bucket zu erhalten

Das folgende list-objects-v2 Beispiel listet die Objekte im angegebenen Bucket auf.

aws s3api list-objects-v2 \ --bucket my-bucket

Ausgabe:

{ "Contents": [ { "LastModified": "2019-11-05T23:11:50.000Z", "ETag": "\"621503c373607d548b37cff8778d992c\"", "StorageClass": "STANDARD", "Key": "doc1.rtf", "Size": 391 }, { "LastModified": "2019-11-05T23:11:50.000Z", "ETag": "\"a2cecc36ab7c7fe3a71a273b9d45b1b5\"", "StorageClass": "STANDARD", "Key": "doc2.rtf", "Size": 373 }, { "LastModified": "2019-11-05T23:11:50.000Z", "ETag": "\"08210852f65a2e9cb999972539a64d68\"", "StorageClass": "STANDARD", "Key": "doc3.rtf", "Size": 399 }, { "LastModified": "2019-11-05T23:11:50.000Z", "ETag": "\"d1852dd683f404306569471af106988e\"", "StorageClass": "STANDARD", "Key": "doc4.rtf", "Size": 6225 } ] }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS CLI Befehlsreferenz.

Go
SDKfür Go V2
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

// BucketBasics encapsulates the Amazon Simple Storage Service (Amazon S3) actions // used in the examples. // It contains S3Client, an Amazon S3 service client that is used to perform bucket // and object actions. type BucketBasics struct { S3Client *s3.Client } // ListObjects lists the objects in a bucket. func (basics BucketBasics) ListObjects(bucketName string) ([]types.Object, error) { result, err := basics.S3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{ Bucket: aws.String(bucketName), }) var contents []types.Object if err != nil { log.Printf("Couldn't list objects in bucket %v. Here's why: %v\n", bucketName, err) } else { contents = result.Contents } return contents, err }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for Go APIReferenz.

Java
SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

/** * Asynchronously lists all objects in the specified S3 bucket. * * @param bucketName the name of the S3 bucket to list objects for * @return a {@link CompletableFuture} that completes when all objects have been listed */ public CompletableFuture<Void> listAllObjectsAsync(String bucketName) { ListObjectsV2Request initialRequest = ListObjectsV2Request.builder() .bucket(bucketName) .maxKeys(1) .build(); ListObjectsV2Publisher paginator = getAsyncClient().listObjectsV2Paginator(initialRequest); return paginator.subscribe(response -> { response.contents().forEach(s3Object -> { logger.info("Object key: " + s3Object.key()); }); }).thenRun(() -> { logger.info("Successfully listed all objects in the bucket: " + bucketName); }).exceptionally(ex -> { throw new RuntimeException("Failed to list objects", ex); }); }

Listet Objekte mithilfe der Paginierung auf.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable; public class ListObjectsPaginated { public static void main(String[] args) { final String usage = """ Usage: <bucketName>\s Where: bucketName - The Amazon S3 bucket from which objects are read.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); listBucketObjects(s3, bucketName); s3.close(); } public static void listBucketObjects(S3Client s3, String bucketName) { try { ListObjectsV2Request listReq = ListObjectsV2Request.builder() .bucket(bucketName) .maxKeys(1) .build(); ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq); listRes.stream() .flatMap(r -> r.contents().stream()) .forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size())); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for Java 2.x APIReferenz.

JavaScript
SDKfür JavaScript (v3)
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

Listen Sie alle Objekte in Ihrem Bucket auf. Wenn es mehr als ein Objekt gibt, IsTruncated NextContinuationToken wird es verwendet, um über die gesamte Liste zu iterieren.

import { S3Client, // This command supersedes the ListObjectsCommand and is the recommended way to list objects. ListObjectsV2Command, } from "@aws-sdk/client-s3"; const client = new S3Client({}); export const main = async () => { const command = new ListObjectsV2Command({ Bucket: "my-bucket", // The default and maximum number of keys returned is 1000. This limits it to // one for demonstration purposes. MaxKeys: 1, }); try { let isTruncated = true; console.log("Your bucket contains the following objects:\n"); let contents = ""; while (isTruncated) { const { Contents, IsTruncated, NextContinuationToken } = await client.send(command); const contentsList = Contents.map((c) => ` • ${c.Key}`).join("\n"); contents += contentsList + "\n"; isTruncated = IsTruncated; command.input.ContinuationToken = NextContinuationToken; } console.log(contents); } catch (err) { console.error(err); } };
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for JavaScript APIReferenz.

Kotlin
SDKfür Kotlin
Anmerkung

Es gibt noch mehr dazu. GitHub Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

suspend fun listBucketObjects(bucketName: String) { val request = ListObjectsRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> val response = s3.listObjects(request) response.contents?.forEach { myObject -> println("The name of the key is ${myObject.key}") println("The object is ${myObject.size?.let { calKb(it) }} KBs") println("The owner is ${myObject.owner}") } } } private fun calKb(intValue: Long): Long = intValue / 1024
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDKals API Kotlin-Referenz.

PHP
SDK für PHP
Anmerkung

Es gibt noch mehr dazu. GitHub Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

Listen Sie Objekte in einem Bucket auf.

$s3client = new Aws\S3\S3Client(['region' => 'us-west-2']); try { $contents = $this->s3client->listObjectsV2([ 'Bucket' => $this->bucketName, ]); echo "The contents of your bucket are: \n"; foreach ($contents['Contents'] as $content) { echo $content['Key'] . "\n"; } } catch (Exception $exception) { echo "Failed to list objects in $this->bucketName with error: " . $exception->getMessage(); exit("Please fix error with listing objects before continuing."); }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for PHP APIReferenz.

Python
SDKfür Python (Boto3)
Anmerkung

Es gibt noch mehr dazu. GitHub Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

class ObjectWrapper: """Encapsulates S3 object actions.""" def __init__(self, s3_object): """ :param s3_object: A Boto3 Object resource. This is a high-level resource in Boto3 that wraps object actions in a class-like structure. """ self.object = s3_object self.key = self.object.key @staticmethod def list(bucket, prefix=None): """ Lists the objects in a bucket, optionally filtered by a prefix. :param bucket: The bucket to query. This is a Boto3 Bucket resource. :param prefix: When specified, only objects that start with this prefix are listed. :return: The list of objects. """ try: if not prefix: objects = list(bucket.objects.all()) else: objects = list(bucket.objects.filter(Prefix=prefix)) logger.info( "Got objects %s from bucket '%s'", [o.key for o in objects], bucket.name ) except ClientError: logger.exception("Couldn't get objects for bucket '%s'.", bucket.name) raise else: return objects
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDKfür Python (Boto3) API -Referenz.

Ruby
SDKfür Ruby
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

require "aws-sdk-s3" # Wraps Amazon S3 bucket actions. class BucketListObjectsWrapper attr_reader :bucket # @param bucket [Aws::S3::Bucket] An existing Amazon S3 bucket. def initialize(bucket) @bucket = bucket end # Lists object in a bucket. # # @param max_objects [Integer] The maximum number of objects to list. # @return [Integer] The number of objects listed. def list_objects(max_objects) count = 0 puts "The objects in #{@bucket.name} are:" @bucket.objects.each do |obj| puts "\t#{obj.key}" count += 1 break if count == max_objects end count rescue Aws::Errors::ServiceError => e puts "Couldn't list objects in bucket #{bucket.name}. Here's why: #{e.message}" 0 end end # Example usage: def run_demo bucket_name = "amzn-s3-demo-bucket" wrapper = BucketListObjectsWrapper.new(Aws::S3::Bucket.new(bucket_name)) count = wrapper.list_objects(25) puts "Listed #{count} objects." end run_demo if $PROGRAM_NAME == __FILE__
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDK for Ruby APIReferenz.

Rust
SDKfür Rust
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

pub async fn list_objects(client: &aws_sdk_s3::Client, bucket: &str) -> Result<(), S3ExampleError> { let mut response = client .list_objects_v2() .bucket(bucket.to_owned()) .max_keys(10) // In this example, go 10 at a time. .into_paginator() .send(); while let Some(result) = response.next().await { match result { Ok(output) => { for object in output.contents() { println!(" - {}", object.key().unwrap_or("Unknown")); } } Err(err) => { eprintln!("{err:?}") } } } Ok(()) }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDKals API Referenz auf Rust.

SAP ABAP
SDKfür SAP ABAP
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

TRY. oo_result = lo_s3->listobjectsv2( " oo_result is returned for testing purposes. " iv_bucket = iv_bucket_name ). MESSAGE 'Retrieved list of objects in S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDKals SAP ABAP API Referenz.

Swift
SDKfür Swift
Anmerkung

Es gibt noch mehr dazu GitHub. Finden Sie das vollständige Beispiel und erfahren Sie, wie Sie es einrichten und ausführen in der AWS Repository mit Codebeispielen.

import AWSS3 public func listBucketFiles(bucket: String) async throws -> [String] { do { let input = ListObjectsV2Input( bucket: bucket ) // Use "Paginated" to get all the objects. // This lets the SDK handle the 'continuationToken' in "ListObjectsV2Output". let output = client.listObjectsV2Paginated(input: input) var names: [String] = [] for try await page in output { guard let objList = page.contents else { print("ERROR: listObjectsV2Paginated returned nil contents.") continue } for obj in objList { if let objName = obj.key { names.append(objName) } } } return names } catch { print("ERROR: ", dump(error, name: "Listing objects.")) throw error } }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in AWS SDKals API Swift-Referenz.

Für eine vollständige Liste von AWS SDKEntwicklerhandbücher und Codebeispiele finden Sie unterVerwenden Sie diesen Dienst mit einem SDK AWS. Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK Versionen.