SDK Amazon S3: Elenca oggetti
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Mostra un esempio di elenco di oggetti per aiutare gli sviluppatori a superare un problema di interoperabilità fino a quando non viene risolto.
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[],[],null,["# Amazon S3 SDK: List objects\n\nShows an example of listing objects to help developers overcome an issue with interoperability until it's fixed.\n\nCode sample\n-----------\n\n### Go\n\n\nFor more information, see the\n[Cloud Storage Go API\nreference documentation](https://pkg.go.dev/cloud.google.com/go/storage).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"github.com/aws/aws-sdk-go/aws\"\n \t\"github.com/aws/aws-sdk-go/aws/credentials\"\n \t\"github.com/aws/aws-sdk-go/aws/session\"\n \t\"github.com/aws/aws-sdk-go/service/s3\"\n )\n\n func listGCSObjects(w io.Writer, bucketName string, googleAccessKeyID string, googleAccessKeySecret string) error {\n \t// bucketName := \"your-gcs-bucket-name\"\n \t// googleAccessKeyID := \"Your Google Access Key ID\"\n \t// googleAccessKeySecret := \"Your Google Access Key Secret\"\n\n \t// Create a new client and do the following:\n \t// 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.\n \t// 2. Use Cloud Storage HMAC Credentials.\n \tsess := session.Must(session.NewSession(&aws.Config{\n \t\tRegion: aws.String(\"auto\"),\n \t\tEndpoint: aws.String(\"https://storage.googleapis.com\"),\n \t\tCredentials: credentials.NewStaticCredentials(googleAccessKeyID, googleAccessKeySecret, \"\"),\n \t}))\n\n \tclient := s3.New(sess)\n \tctx := context.Background()\n\n \tresult, err := client.ListObjectsWithContext(ctx, &s3.ListObjectsInput{\n \t\tBucket: aws.String(bucketName),\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"ListObjectsWithContext: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Objects:\")\n \tfor _, o := range result.Contents {\n \t\tfmt.Fprintf(w, \"%s\\n\", aws.StringValue(o.Key))\n \t}\n\n \treturn nil\n }\n\n### Java\n\n\nFor more information, see the\n[Cloud Storage Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import com.amazonaws.auth.AWSStaticCredentialsProvider;\n import com.amazonaws.auth.BasicAWSCredentials;\n import com.amazonaws.client.builder.AwsClientBuilder;\n import com.amazonaws.services.s3.AmazonS3;\n import com.amazonaws.services.s3.AmazonS3ClientBuilder;\n import com.amazonaws.services.s3.model.ObjectListing;\n import com.amazonaws.services.s3.model.S3ObjectSummary;\n\n public class ListGcsObjects {\n public static void listGcsObjects(\n String googleAccessKeyId, String googleAccessKeySecret, String bucketName) {\n\n // String googleAccessKeyId = \"your-google-access-key-id\";\n // String googleAccessKeySecret = \"your-google-access-key-secret\";\n // String bucketName = \"bucket-name\";\n\n // Create a BasicAWSCredentials using Cloud Storage HMAC credentials.\n BasicAWSCredentials googleCreds =\n new BasicAWSCredentials(googleAccessKeyId, googleAccessKeySecret);\n\n // Create a new client and do the following:\n // 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.\n // 2. Use Cloud Storage HMAC Credentials.\n AmazonS3 interopClient =\n AmazonS3ClientBuilder.standard()\n .withEndpointConfiguration(\n new AwsClientBuilder.EndpointConfiguration(\n \"https://storage.googleapis.com\", \"auto\"))\n .withCredentials(new AWSStaticCredentialsProvider(googleCreds))\n .build();\n\n // Call GCS to list current objects\n ObjectListing objects = interopClient.listObjects(bucketName);\n\n // Print objects names\n System.out.println(\"Objects:\");\n for (S3ObjectSummary object : objects.getObjectSummaries()) {\n System.out.println(object.getKey());\n }\n\n // Explicitly clean up client resources.\n interopClient.shutdown();\n }\n }\n\n### Python\n\n\nFor more information, see the\n[Cloud Storage Python API\nreference documentation](https://cloud.google.com/python/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import boto3 # type: ignore\n\n\n def list_gcs_objects(\n google_access_key_id: str, google_access_key_secret: str, bucket_name: str\n ) -\u003e List[str]:\n \"\"\"Lists all Cloud Storage objects using AWS SDK for Python (boto3)\n Positional arguments:\n google_access_key_id: hash-based message authentication code (HMAC) access ID\n google_access_key_secret: HMAC access secret\n bucket_name: name of Cloud Storage bucket\n\n Returned value is a list of strings, one for each object in the bucket.\n\n To use this sample:\n 1. Create a Cloud Storage HMAC key: https://cloud.google.com/storage/docs/authentication/managing-hmackeys#create\n 2. Change endpoint_url to a Google Cloud Storage XML API endpoint.\n\n To learn more about HMAC: https://cloud.google.com/storage/docs/authentication/hmackeys#overview\n \"\"\"\n client = boto3.client(\n \"s3\",\n region_name=\"auto\",\n endpoint_url=\"https://storage.googleapis.com\",\n aws_access_key_id=google_access_key_id,\n aws_secret_access_key=google_access_key_secret,\n )\n\n # Call GCS to list objects in bucket_name\n response = client.list_objects(Bucket=bucket_name)\n\n # Return list of object names in bucket\n results = []\n for blob in response[\"Contents\"]:\n results.append(blob[\"Key\"])\n print(blob[\"Key\"]) # Can remove if not needed after development\n return results\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]