zoukankan      html  css  js  c++  java
  • amazon-aws 使用 SNS 发送短信

    jar-maven 

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-sns -->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sns</artifactId>
        <version>1.11.475</version>
    </dependency>

    java:

    import com.amazonaws.auth.AWSCredentials;
    import com.amazonaws.auth.AWSCredentialsProvider;
    import com.amazonaws.services.sns.AmazonSNS;
    import com.amazonaws.services.sns.AmazonSNSClientBuilder;
    import com.amazonaws.services.sns.model.MessageAttributeValue;
    import com.amazonaws.services.sns.model.PublishRequest;
    import com.amazonaws.services.sns.model.PublishResult;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class ShortMessage {
        private Map<String, MessageAttributeValue> smsAttributes;
    
    
        public Map<String, MessageAttributeValue> getDefaultSMSAttributes() {
            if (smsAttributes == null) {
                smsAttributes = new HashMap<>();
                smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
                        .withStringValue("1")
                        .withDataType("String"));
                smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue()
                        .withStringValue("0.05")
                        .withDataType("Number"));
                smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue()
                        .withStringValue("Transactional")
                        .withDataType("String"));
            }
            return smsAttributes;
        }
    
        public PublishResult sendSMSMessage(String phoneNumber, String message) {
            return sendSMSMessage(phoneNumber, message, getDefaultSMSAttributes());
        }
    
        public PublishResult sendSMSMessage(String phoneNumber, String message, Map<String, MessageAttributeValue> smsAttributes) {
            AWSCredentials awsCredentials = new AWSCredentials() {
                @Override
                public String getAWSAccessKeyId() {
                    return "xxxxxxxx"; // 带有发短信权限的 IAM 的 ACCESS_KEY
                }
    
                @Override
                public String getAWSSecretKey() {
                    return "xxxxxxxx"; // 带有发短信权限的 IAM 的 SECRET_KEY
                }
            };
            AWSCredentialsProvider provider = new AWSCredentialsProvider() {
                @Override
                public AWSCredentials getCredentials() {
                    return awsCredentials;
                }
    
                @Override
                public void refresh() {
                }
            };
            AmazonSNS amazonSNS = null;
            try {
                amazonSNS = AmazonSNSClientBuilder.standard().withCredentials(provider).withRegion("us-east-1").build();
            } catch (Exception e) {
    
            }
            return amazonSNS.publish(
                    new PublishRequest()
                            .withMessage(message)
                            .withPhoneNumber(phoneNumber)
                            .withMessageAttributes(smsAttributes)
            );
        }
    
        public static void main(String[] args) {
            //AmazonSNSClient client = new AmazonSNSClient(credentialsProvider);
            //AWSCredentialsProviderChain chain = new AWSCredentialsProviderChain(credentialsProvider);
            //chain.setReuseLastProvider(true);
            //credentialsProvider.getCredentials();
            ShortMessage shortMessage = new ShortMessage();
            PublishResult publishResult = shortMessage.sendSMSMessage("+8613958942952", "test1");
            System.out.println(publishResult);
        }
    
    } 

    原文:https://blog.csdn.net/qq_33811662/article/details/80809849

  • 相关阅读:
    各种视角了解数组Array以及案例展示
    js基础面试高频面点2:Javascript中undefined和not defined有什么区别,和null又有什么联系?
    一些好用的Linux命令工具
    Linux实用命令工具-dtrx根据需要自动解压
    J2EE--Hibernate基础笔记
    mysql用户与权限管理笔记
    找出字符串中出现频率最少的字符,并将其去除
    Linux常用命令--文件(夹)查找之find命令
    J2EE--Struts2基础开发笔记
    Java多线程基础知识总结笔记
  • 原文地址:https://www.cnblogs.com/feecy/p/10242102.html
Copyright © 2011-2022 走看看