Menggunakan API S3 Tingkat Layanan - AWS Mobile SDK

AWS Mobile SDK for Xamarin sekarang disertakan dalam AWS SDK for .NET. Panduan ini menjadi referensi versi yang diarsipkan dari Mobile SDK for Xamarin.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Menggunakan API S3 Tingkat Layanan

Selain menggunakan S3TransferUtility, Anda juga dapat berinteraksi dengan S3 dengan menggunakan API S3 tingkat rendah.

Menginisialisasi Amazon S3 Client

Untuk menggunakan Amazon S3, pertama kita perlu membuat instans AmazonS3Client yang mengambil referensi ke CognitoAWSCredentialscontoh yang Anda buat sebelumnya dan wilayah Anda:

AmazonS3Client S3Client = new AmazonS3Client (credentials,region);

Mengunduh File

Untuk mengunduh file dari S3:

// Create a GetObject request GetObjectRequest request = new GetObjectRequest { BucketName = "SampleBucket", Key = "Item1" }; // Issue request and remember to dispose of the response using (GetObjectResponse response = client.GetObject(request)) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { string contents = reader.ReadToEnd(); Console.WriteLine("Object - " + response.Key); Console.WriteLine(" Version Id - " + response.VersionId); Console.WriteLine(" Contents - " + contents); } }

Meng-unggah File

Untuk meng-unggah file ke S3:

// Create a client AmazonS3Client client = new AmazonS3Client(); // Create a PutObject request PutObjectRequest request = new PutObjectRequest { BucketName = "SampleBucket", Key = "Item1", FilePath = "contents.txt" }; // Put object PutObjectResponse response = client.PutObject(request);

Hapus Item

Untuk menghapus item di S3:

// Create a client AmazonS3Client client = new AmazonS3Client(); // Create a DeleteObject request DeleteObjectRequest request = new DeleteObjectRequest { BucketName = "SampleBucket", Key = "Item1" }; // Issue request client.DeleteObject(request);

Menghapus Banyak Item

Untuk menghapus beberapa objek dari bucket menggunakan permintaan HTTP tunggal:

// Create a client AmazonS3Client client = new AmazonS3Client(); // Create a DeleteObject request DeleteObjectsRequest request = new DeleteObjectsRequest { BucketName = "SampleBucket", Objects = new List<KeyVersion> { new KeyVersion() {Key = "Item1"}, // Versioned item new KeyVersion() { Key = "Item2", VersionId = "Rej8CiBxcZKVK81cLr39j27Y5FVXghDK", }, // Item in subdirectory new KeyVersion() { Key = "Logs/error.txt"} } }; try { // Issue request DeleteObjectsResponse response = client.DeleteObjects(request); } catch (DeleteObjectsException doe) { // Catch error and list error details DeleteObjectsResponse errorResponse = doe.Response; foreach (DeletedObject deletedObject in errorResponse.DeletedObjects) { Console.WriteLine("Deleted item " + deletedObject.Key); } foreach (DeleteError deleteError in errorResponse.DeleteErrors) { Console.WriteLine("Error deleting item " + deleteError.Key); Console.WriteLine(" Code - " + deleteError.Code); Console.WriteLine(" Message - " + deleteError.Message); } }

Anda dapat menentukan hingga 1000 kunci.

Buat Daftar Bucket

Berikan izin untuk mencantumkan semua bucket yang dimiliki oleh pengirim permintaan yang di-autentikasi:

// Create a client AmazonS3Client client = new AmazonS3Client(); // Issue call ListBucketsResponse response = client.ListBuckets(); // View response data Console.WriteLine("Buckets owner - {0}", response.Owner.DisplayName); foreach (S3Bucket bucket in response.Buckets) { Console.WriteLine("Bucket {0}, Created on {1}", bucket.BucketName, bucket.CreationDate); }

Daftar Objek

Anda dapat mengembalikan beberapa atau semua (hingga 1000) objek yang disimpan dalam bucket S3 Anda. Untuk melakukannya, Anda harus memiliki akses baca ke bucket.

// Create a GetObject request GetObjectRequest request = new GetObjectRequest { BucketName = "SampleBucket", Key = "Item1" }; // Issue request and remember to dispose of the response using (GetObjectResponse response = client.GetObject(request)) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { string contents = reader.ReadToEnd(); Console.WriteLine("Object - " + response.Key); Console.WriteLine(" Version Id - " + response.VersionId); Console.WriteLine(" Contents - " + contents); } }

Dapatkan Wilayah Bucket

Untuk mendapatkan wilayah tempat bucket berada:

// Create a client AmazonS3Client client = new AmazonS3Client(); // Construct request GetBucketLocationRequest request = new GetBucketLocationRequest { BucketName = "SampleBucket" }; // Issue call GetBucketLocationResponse response = client.GetBucketLocation(request); // View response data Console.WriteLine("Bucket location - {0}", response.Location);

Dapatkan Kebijakan Bucket

Untuk mendapatkan kebijakan bucket:

// Create a client AmazonS3Client client = new AmazonS3Client(); // Construct request GetBucketPolicyRequest getRequest = new GetBucketPolicyRequest { BucketName = "SampleBucket" }; string policy = client.GetBucketPolicy(getRequest).Policy; Console.WriteLine(policy); Debug.Assert(policy.Contains("BasicPerms"));