使用aws短信服务踩坑
1.调用aws短信api
//亚马逊短信
func (s basicService) SendAliSMSForVietnam(ctx context.Context, req *pb.SMSRequest) (response *pb.SMSResponse, err error) {
response = new(pb.SMSResponse)
Msg := fmt.Sprintf("[Tenda] Mã xác thực TDSEE là: %s. Vui lòng sử dụng trong 5 phút.", req.Code)
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(s.conf.AwsSMS.AwsRegion),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(s.conf.AwsSMS.AwsAccessKeyId, s.conf.AwsSMS.AwsSecretAccessKey, "")),
)
if err != nil {
panic("configuration error, " + err.Error())
}
mylog.Debug(fmt.Sprintf("cfg=%v", cfg))
client := sns.NewFromConfig(cfg)
params := &sns.PublishInput{
Message: &Msg,
PhoneNumber: &req.Phone,
}
mylog.Debug(fmt.Sprintf("input=%v", params))
mylog.Debug(fmt.Sprintf("client=%v", client))
// 发送短信
resp, err := client.Publish(context.TODO(), params)
if err != nil {
mylog.Error("failed to send SMS, " + err.Error())
response.RespCode = projectecode.INTERNAL_ERR
return
}
mylog.Debug(fmt.Sprintf("result=%v", resp))
fmt.Println("Message ID: " + *resp.MessageId)
return
}
2.使用s3储存桶存放发送日志
1.新建存储桶
2.使用如下连接进行配置https://docs.aws.amazon.com/zh_cn/sns/latest/dg/sms_stats_usage.html
https://docs.aws.amazon.com/zh_cn/sns/latest/dg/sms_stats_usage.html#example_bucket_policy(替换bucket及account_id)