将服务替换为 npm 模块
Atlas Device Sync 、 Atlas Edge Server 、 Data API和HTTPS endpoints均已弃用。 有关详细信息,请参阅弃用页面。
Overview
您应迁移已弃用的第三方服务集成,以使用 npm 提供的相应官方库 。本页后面的部分包含每项服务的详细信息和示例。
要从已弃用的服务迁移,请执行以下操作:
将来自
npm
的服务的库添加到您的应用程序。在函数中导入库。 将对内置服务操作的任何调用替换为对导入库中相应方法的调用。
如果服务具有 Webhook,请将其转换为 HTTPS 端点。
HTTP Service
HTTP将通过HTTP Service 客户端发送的 请求替换为对HTTP 库(例如 node-fetch )的调用 或 axios。
node-fetch
npm i node-fetch@^2
重要
Atlas App Services 不支持 v3 的node-fetch
。 请改用 v2。
之前
exports = async function() { const http = context.services.get("myHttp"); const response = await http.get({ url: "https://www.example.com", headers: { "Content-Type": [ "application/json" ] } }) return response.body.text() }
之后(节点获取)
exports = async function() { const fetch = require("node-fetch"); // require calls must be in exports function const response = await fetch("https://www.example.com", { method: "GET", headers: { "Content-Type": "application/json" } }) return response.text() }
axios
npm i axios
之前
exports = async function() { const http = context.services.get("myHttp"); const response = await http.get({ url: "https://www.example.com", headers: { "Content-Type": [ "application/json" ] } }) return response.body.text() }
之后 (axios)
exports = async function() { const axios = require("axios"); // require calls must be in exports function const response = await axios.get("https://www.example.com", { headers: { "Content-Type": "application/json" } }) return response.data }
Twilio 服务
将通过 Twilio 服务 客户端的调用替换为对官方 Twilio 节点助手库的调用。
npm i twilio
要对 Twilio 请求进行身份验证,请将您的帐户 SID 和身份验证令牌存储为values 。 然后,您可以在函数中访问它们并将它们传递给 SDK。
之前
exports = async function() { const twilio = context.services.get("myTwilio"); twilio.send({ to: "+15558675309", from: "+15551234567", body: "Hello from App Services!", }); }
之后
exports = async function() { // Note: require calls must be in the exported function const twilio = require('twilio')( // Your Account SID and Auth Token from https://www.twilio.com/console // Specify the same Twilio credentials as the service configuration context.values.get("TwilioAccountSID"), context.values.get("TwilioAuthToken"), ) await twilio.messages.create({ to: "+15558675309", from: "+15551234567", body: "Hello from App Services!", }) }
Amazon Web Services 服务
Amazon Web Services将通过 服务 客户端的调用替换为对官方Amazon Web ServicesJavaScript SDK 的调用。
npm i aws-sdk
重要
App Services 尚不支持 Amazon Web Services SDK 版本 3。使用版本 2 SDK 替换函数中已弃用的 Amazon Web Services 服务。
要对 Amazon Web Services 请求进行身份验证,请将访问密钥 ID 和秘密访问密钥存储为值。然后,您可以在函数中访问它们并将它们传递给 SDK。
S3
之前
exports = async function() { const s3 = context.services.get("myAWS").s3("us-east-1"); const putResult = await s3.PutObject({ Bucket: "bucketName", Key: "keyName", Body: EJSON.stringify({ hello: "world" }), }); const getResult = await s3.GetObject({ Bucket: "bucketName", Key: "keyName", }); }
之后
exports = async function() { const S3 = require('aws-sdk/clients/s3'); // require calls must be in exports function const s3 = new S3({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property const putResult = await s3.putObject({ Bucket: "bucketName", Key: "keyName", Body: EJSON.stringify({ hello: "world" }), }).promise(); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property const getResult = await s3.getObject({ Bucket: "bucketName", Key: "keyName", }).promise(); }
Kinesis
之前
exports = async function() { const kinesis = context.services.get("myAWS").kinesis("us-east-1"); const putResult = await kinesis.PutRecord({ Data: EJSON.stringify({ hello: "world" }), StreamName: "myStream", PartitionKey: "myPartition", }); }
之后
exports = async function() { const Kinesis = require('aws-sdk/clients/kinesis'); // require calls must be in exports function const kinesis = new Kinesis({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html#putRecord-property const putResult = await kinesis.putRecord({ Data: EJSON.stringify({ hello: "world" }), StreamName: "myStream", PartitionKey: "myPartition", }).promise(); }
Lambda
之前
exports = async function() { const lambda = context.services.get('MyAwsService').lambda("us-east-1"); const invokeResult = await lambda.Invoke({ FunctionName: "myLambdaFunction", Payload: EJSON.stringify({ hello: "world" }), }); };
之后
exports = async function() { const Lambda = require('aws-sdk/clients/lambda'); // require calls must be in exports function const lambda = new Lambda({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property const invokeResult = await lambda.invoke({ FunctionName: "myLambdaFunction", Payload: EJSON.stringify({ hello: "world" }), }).promise(); }
SES
之前
exports = async function() { const ses = context.services.get('MyAwsService').ses("us-east-1"); const sendResult = await ses.SendEmail({ Source: "sender@example.com", Destination: { ToAddresses: ["receiver@example.com"] }, Message: { Body: { Html: { Charset: "UTF-8", Data: `This is a message from user ${context.user.id} sent through AWS SES` } }, Subject: { Charset: "UTF-8", Data: "Test Email Please Ignore" }, }, }); };
之后
exports = async function() { const SES = require('aws-sdk/clients/ses'); // require calls must be in exports function const ses = new SES({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html#sendEmail-property const sendResult = await ses.sendEmail({ Source: "sender@example.com", Destination: { ToAddresses: ["receiver@example.com"] }, Message: { Body: { Html: { Charset: "UTF-8", Data: `This is a message from user ${context.user.id} sent through AWS SES` } }, Subject: { Charset: "UTF-8", Data: "Test Email Please Ignore" }, }, }).promise(); }