D'autres exemples de AWS SDK sont disponibles dans le référentiel AWSDoc SDK Examples GitHub .
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Abonner un point de terminaison HTTP à une rubrique Amazon SNS à l’aide d’un AWS kit SDK
Les exemples de code suivants montrent comment s’abonner à un point de terminaison HTTP ou HTTPS afin qu’il reçoive des notifications d’une rubrique Amazon SNS.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action dans son contexte dans les exemples de code suivants :
- Java
-
- Kit SDK pour Java 2.x
-
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.SnsException;
import software.amazon.awssdk.services.sns.model.SubscribeRequest;
import software.amazon.awssdk.services.sns.model.SubscribeResponse;
/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class SubscribeHTTPS {
public static void main(String[] args) {
final String usage = """
Usage: <topicArn> <url>
Where:
topicArn - The ARN of the topic to subscribe.
url - The HTTPS endpoint that you want to receive notifications.
""";
if (args.length < 2) {
System.out.println(usage);
System.exit(1);
}
String topicArn = args[0];
String url = args[1];
SnsClient snsClient = SnsClient.builder()
.region(Region.US_EAST_1)
.build();
subHTTPS(snsClient, topicArn, url);
snsClient.close();
}
public static void subHTTPS(SnsClient snsClient, String topicArn, String url) {
try {
SubscribeRequest request = SubscribeRequest.builder()
.protocol("https")
.endpoint(url)
.returnSubscriptionArn(true)
.topicArn(topicArn)
.build();
SubscribeResponse result = snsClient.subscribe(request);
System.out.println("Subscription ARN is " + result.subscriptionArn() + "\n\n Status is "
+ result.sdkHttpResponse().statusCode());
} catch (SnsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
- PHP
-
- Kit SDK pour PHP
-
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
/**
* Prepares to subscribe an endpoint by sending the endpoint a confirmation message.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$protocol = 'https';
$endpoint = 'https://';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}