Contoh Amazon SNS menggunakan AWS CLI - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

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

Contoh Amazon SNS menggunakan AWS CLI

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS Command Line Interface dengan Amazon SNS.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Skenario adalah contoh kode yang menunjukkan kepada Anda bagaimana menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan atau dikombinasikan dengan yang lain Layanan AWS.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanadd-permission.

AWS CLI

Untuk menambahkan izin ke topik

add-permissionContoh berikut menambahkan izin untuk AWS akun 987654321098 untuk menggunakan Publish tindakan dengan topik yang ditentukan di bawah AWS akun123456789012.

aws sns add-permission \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --label Publish-Permission \ --aws-account-id 987654321098 \ --action-name Publish

Perintah ini tidak menghasilkan output.

  • Untuk detail API, lihat AddPermissiondi Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakancheck-if-phone-number-is-opted-out.

AWS CLI

Untuk memeriksa pesan SMS opt-out untuk nomor telepon

check-if-phone-number-is-opted-outContoh berikut memeriksa apakah nomor telepon yang ditentukan dipilih untuk tidak menerima pesan SMS dari AWS akun saat ini.

aws sns check-if-phone-number-is-opted-out \ --phone-number +1555550100

Output:

{ "isOptedOut": false }

Contoh kode berikut menunjukkan cara menggunakanconfirm-subscription.

AWS CLI

Untuk mengonfirmasi langganan

confirm-subscriptionPerintah berikut menyelesaikan proses konfirmasi yang dimulai saat Anda berlangganan topik SNS bernama. my-topic Parameter --token berasal dari pesan konfirmasi yang dikirim ke titik akhir notifikasi yang ditentukan dalam panggilan berlangganan.

aws sns confirm-subscription \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --token 2336412f37fb687f5d51e6e241d7700ae02f7124d8268910b858cb4db727ceeb2474bb937929d3bdd7ce5d0cce19325d036bc858d3c217426bcafa9c501a2cace93b83f1dd3797627467553dc438a8c974119496fc3eff026eaa5d14472ded6f9a5c43aec62d83ef5f49109da7176391

Output:

{ "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" }

Contoh kode berikut menunjukkan cara menggunakancreate-platform-application.

AWS CLI

Untuk membuat aplikasi platform

create-platform-applicationContoh berikut membuat aplikasi platform Google Firebase menggunakan kredensi platform yang ditentukan.

aws sns create-platform-application \ --name MyApplication \ --platform GCM \ --attributes PlatformCredential=EXAMPLEabcd12345jklm67890stuv12345bcdef

Output:

{ "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication" }

Contoh kode berikut menunjukkan cara menggunakancreate-topic.

AWS CLI

Untuk membuat topik SNS

create-topicContoh berikut membuat topik SNS bernamamy-topic.

aws sns create-topic \ --name my-topic

Output:

{ "ResponseMetadata": { "RequestId": "1469e8d7-1642-564e-b85d-a19b4b341f83" }, "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" }

Untuk informasi selengkapnya, lihat Menggunakan Antarmuka Baris AWS Perintah dengan Amazon SQS dan Amazon SNS di Panduan Pengguna Antarmuka Baris AWS Perintah.

  • Untuk detail API, lihat CreateTopicdi Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakandelete-endpoint.

AWS CLI

Untuk menghapus titik akhir aplikasi platform

delete-endpointContoh berikut menghapus endpoint aplikasi platform yang ditentukan.

aws sns delete-endpoint \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234

Perintah ini tidak menghasilkan output.

  • Untuk detail API, lihat DeleteEndpointdi Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakandelete-platform-application.

AWS CLI

Untuk menghapus aplikasi platform

delete-platform-applicationContoh berikut menghapus aplikasi platform yang ditentukan.

aws sns delete-platform-application \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakandelete-topic.

AWS CLI

Untuk menghapus topik SNS

delete-topicContoh berikut menghapus topik SNS yang ditentukan.

aws sns delete-topic \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

Perintah ini tidak menghasilkan output.

  • Untuk detail API, lihat DeleteTopicdi Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakanget-endpoint-attributes.

AWS CLI

Untuk membuat daftar atribut endpoint aplikasi platform

get-endpoint-attributesContoh berikut mencantumkan atribut untuk endpoint aplikasi platform tertentu.

aws sns get-endpoint-attributes \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234

Output:

{ "Attributes": { "Enabled": "true", "Token": "EXAMPLE12345..." } }

Contoh kode berikut menunjukkan cara menggunakanget-platform-application-attributes.

AWS CLI

Untuk membuat daftar atribut aplikasi platform

get-platform-application-attributesContoh berikut mencantumkan atribut untuk aplikasi platform tertentu.

aws sns get-platform-application-attributes \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/MPNS/MyApplication

Output:

{ "Attributes": { "Enabled": "true", "SuccessFeedbackSampleRate": "100" } }

Contoh kode berikut menunjukkan cara menggunakanget-sms-attributes.

AWS CLI

Untuk mencantumkan atribut pesan SMS default

get-sms-attributesContoh berikut mencantumkan atribut default untuk mengirim pesan SMS.

aws sns get-sms-attributes

Output:

{ "attributes": { "DefaultSenderID": "MyName" } }

Contoh kode berikut menunjukkan cara menggunakanget-subscription-attributes.

AWS CLI

Untuk mengambil atribut langganan untuk topik

Berikut ini get-subscription-attributes menampilkan atribut langganan yang ditentukan. Anda bisa mendapatkan subscription-arn dari output list-subscriptions perintah.

aws sns get-subscription-attributes \ --subscription-arn "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f"

Output:

{ "Attributes": { "Endpoint": "my-email@example.com", "Protocol": "email", "RawMessageDelivery": "false", "ConfirmationWasAuthenticated": "false", "Owner": "123456789012", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } }

Contoh kode berikut menunjukkan cara menggunakanget-topic-attributes.

AWS CLI

Untuk mengambil atribut dari suatu topik

get-topic-attributesContoh berikut menampilkan atribut untuk topik yang ditentukan.

aws sns get-topic-attributes \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

Output:

{ "Attributes": { "SubscriptionsConfirmed": "1", "DisplayName": "my-topic", "SubscriptionsDeleted": "0", "EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", "Owner": "123456789012", "Policy": "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:DeleteTopic\",\"SNS:GetTopicAttributes\",\"SNS:Publish\",\"SNS:RemovePermission\",\"SNS:AddPermission\",\"SNS:SetTopicAttributes\"],\"Resource\":\"arn:aws:sns:us-west-2:123456789012:my-topic\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"0123456789012\"}}}]}", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionsPending": "0" } }

Contoh kode berikut menunjukkan cara menggunakanlist-endpoints-by-platform-application.

AWS CLI

Untuk membuat daftar titik akhir untuk aplikasi platform

list-endpoints-by-platform-applicationContoh berikut mencantumkan titik akhir dan atribut endpoint untuk aplikasi platform tertentu.

aws sns list-endpoints-by-platform-application \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication

Output:

{ "Endpoints": [ { "Attributes": { "Token": "EXAMPLE12345..., "Enabled": "true" }, "EndpointArn": "arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" } ] }

Contoh kode berikut menunjukkan cara menggunakanlist-phone-numbers-opted-out.

AWS CLI

Untuk membuat daftar opt-out pesan SMS

list-phone-numbers-opted-outContoh berikut mencantumkan nomor telepon yang dipilih untuk tidak menerima pesan SMS.

aws sns list-phone-numbers-opted-out

Output:

{ "phoneNumbers": [ "+15555550100" ] }

Contoh kode berikut menunjukkan cara menggunakanlist-platform-applications.

AWS CLI

Untuk membuat daftar aplikasi platform

list-platform-applicationsContoh berikut mencantumkan aplikasi platform untuk ADM dan MPNS.

aws sns list-platform-applications

Output:

{ "PlatformApplications": [ { "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication", "Attributes": { "SuccessFeedbackSampleRate": "100", "Enabled": "true" } }, { "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/MPNS/MyOtherApplication", "Attributes": { "SuccessFeedbackSampleRate": "100", "Enabled": "true" } } ] }

Contoh kode berikut menunjukkan cara menggunakanlist-subscriptions-by-topic.

AWS CLI

Untuk membuat daftar langganan yang terkait dengan topik

Berikut ini list-subscriptions-by-topic mengambil daftar langganan SNS yang terkait dengan topik yang ditentukan.

aws sns list-subscriptions-by-topic \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

Output:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }

Contoh kode berikut menunjukkan cara menggunakanlist-subscriptions.

AWS CLI

Untuk membuat daftar langganan SNS Anda

list-subscriptionsContoh berikut menampilkan daftar langganan SNS di akun Anda AWS .

aws sns list-subscriptions

Output:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }

Contoh kode berikut menunjukkan cara menggunakanlist-tags-for-resource.

AWS CLI

Untuk mencantumkan tag untuk suatu topik

list-tags-for-resourceContoh berikut mencantumkan tag untuk topik Amazon SNS yang ditentukan.

aws sns list-tags-for-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic

Output:

{ "Tags": [ { "Key": "Team", "Value": "Alpha" } ] }

Contoh kode berikut menunjukkan cara menggunakanlist-topics.

AWS CLI

Untuk membuat daftar topik SNS Anda

list-topicsContoh berikut mencantumkan semua topik SNS di AWS akun Anda.

aws sns list-topics

Output:

{ "Topics": [ { "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } ] }
  • Untuk detail API, lihat ListTopicsdi Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakanopt-in-phone-number.

AWS CLI

Untuk ikut serta dalam pesan SMS

opt-in-phone-numberContoh berikut memilih nomor telepon yang ditentukan untuk menerima pesan SMS.

aws sns opt-in-phone-number \ --phone-number +15555550100

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanpublish.

AWS CLI

Contoh 1: Untuk mempublikasikan pesan ke topik

publishContoh berikut menerbitkan pesan yang ditentukan ke topik SNS yang ditentukan. Pesan berasal dari file teks, yang memungkinkan Anda untuk memasukkan jeda baris.

aws sns publish \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic" \ --message file://message.txt

Isi dari message.txt:

Hello World Second Line

Output:

{ "MessageId": "123a45b6-7890-12c3-45d6-111122223333" }

Contoh 2: Untuk mempublikasikan pesan SMS ke nomor telepon

publishContoh berikut menerbitkan pesan Hello world! ke nomor +1-555-555-0100 telepon.

aws sns publish \ --message "Hello world!" \ --phone-number +1-555-555-0100

Output:

{ "MessageId": "123a45b6-7890-12c3-45d6-333322221111" }
  • Untuk detail API, lihat Menerbitkan di Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakanput-data-protection-policy.

AWS CLI

Untuk menetapkan kebijakan perlindungan data

Contoh 1: Untuk menolak penerbit mempublikasikan pesan dengan CreditCardNumber

put-data-protection-policyContoh berikut menyangkal penerbit dari mempublikasikan pesan dengan. CreditCardNumber

aws sns put-data-protection-policy \ --resource-arn arn:aws:sns:us-east-1:123456789012:mytopic \ --data-protection-policy "{\"Name\":\"data_protection_policy\",\"Description\":\"Example data protection policy\",\"Version\":\"2021-06-01\",\"Statement\":[{\"DataDirection\":\"Inbound\",\"Principal\":[\"*\"],\"DataIdentifier\":[\"arn:aws:dataprotection::aws:data-identifier/CreditCardNumber\"],\"Operation\":{\"Deny\":{}}}]}"

Perintah ini tidak menghasilkan output.

Contoh 2: Untuk memuat parameter dari file

Berikut ini put-data-protection-policy memuat parameter dari file.

aws sns put-data-protection-policy \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --data-protection-policy file://policy.json

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanremove-permission.

AWS CLI

Untuk menghapus izin dari topik

remove-permissionContoh berikut menghapus izin Publish-Permission dari topik yang ditentukan.

aws sns remove-permission \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --label Publish-Permission

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanset-endpoint-attributes.

AWS CLI

Untuk mengatur atribut titik akhir

set-endpoint-attributesContoh berikut menonaktifkan endpoint aplikasi platform yang ditentukan.

aws sns set-endpoint-attributes \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234 \ --attributes Enabled=false

Output:

{ "Attributes": { "Enabled": "false", "Token": "EXAMPLE12345..." } }

Contoh kode berikut menunjukkan cara menggunakanset-platform-application-attributes.

AWS CLI

Untuk mengatur atribut aplikasi platform

set-platform-application-attributesContoh berikut menetapkan EventDeliveryFailure atribut untuk aplikasi platform yang ditentukan ke ARN dari topik Amazon SNS yang ditentukan.

aws sns set-platform-application-attributes \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication \ --attributes EventDeliveryFailure=arn:aws:sns:us-west-2:123456789012:AnotherTopic

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanset-sms-attributes.

AWS CLI

Untuk mengatur atribut pesan SMS

set-sms-attributesContoh berikut menetapkan ID pengirim default untuk pesan SMS keMyName.

aws sns set-sms-attributes \ --attributes DefaultSenderID=MyName

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanset-subscription-attributes.

AWS CLI

Untuk mengatur atribut langganan

set-subscription-attributesContoh berikut menetapkan RawMessageDelivery atribut ke langganan SQS.

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name RawMessageDelivery \ --attribute-value true

Perintah ini tidak menghasilkan output.

set-subscription-attributesContoh berikut menetapkan FilterPolicy atribut ke langganan SQS.

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name FilterPolicy \ --attribute-value "{ \"anyMandatoryKey\": [\"any\", \"of\", \"these\"] }"

Perintah ini tidak menghasilkan output.

set-subscription-attributesContoh berikut menghapus FilterPolicy atribut dari langganan SQS.

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name FilterPolicy \ --attribute-value "{}"

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanset-topic-attributes.

AWS CLI

Untuk menetapkan atribut untuk topik

set-topic-attributesContoh berikut menetapkan DisplayName atribut untuk topik tertentu.

aws sns set-topic-attributes \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --attribute-name DisplayName \ --attribute-value MyTopicDisplayName

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakansubscribe.

AWS CLI

Untuk berlangganan topik

subscribePerintah berikut berlangganan alamat email ke topik yang ditentukan.

aws sns subscribe \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --protocol email \ --notification-endpoint my-email@example.com

Output:

{ "SubscriptionArn": "pending confirmation" }
  • Untuk detail API, lihat Berlangganan di Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakantag-resource.

AWS CLI

Untuk menambahkan tag ke topik

tag-resourceContoh berikut menambahkan tag metadata ke topik Amazon SNS yang ditentukan.

aws sns tag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tags Key=Team,Value=Alpha

Perintah ini tidak menghasilkan output.

  • Untuk detail API, lihat TagResourcedi Referensi AWS CLI Perintah.

Contoh kode berikut menunjukkan cara menggunakanunsubscribe.

AWS CLI

Untuk berhenti berlangganan dari suatu topik

unsubscribeContoh berikut menghapus langganan yang ditentukan dari suatu topik.

aws sns unsubscribe \ --subscription-arn arn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f

Perintah ini tidak menghasilkan output.

Contoh kode berikut menunjukkan cara menggunakanuntag-resource.

AWS CLI

Untuk menghapus tag dari topik

untag-resourceContoh berikut menghapus tag apa pun dengan kunci yang ditentukan dari topik Amazon SNS yang ditentukan.

aws sns untag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tag-keys Team

Perintah ini tidak menghasilkan output.

  • Untuk detail API, lihat UntagResourcedi Referensi AWS CLI Perintah.

Skenario

Contoh kode berikut menunjukkan cara membuat titik akhir platform untuk notifikasi push Amazon SNS.

AWS CLI

Untuk membuat endpoint aplikasi platform

create-platform-endpointContoh berikut membuat titik akhir untuk aplikasi platform tertentu menggunakan token yang ditentukan.

aws sns create-platform-endpoint \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication \ --token EXAMPLE12345...

Output:

{ "EndpointArn": "arn:aws:sns:us-west-2:1234567890:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" }