Docs 菜单

Docs 主页Atlas App Services

Amazon Web Services S3 代码片段 [已弃用]

在此页面上

  • 概述
  • 将图像上传到 S3
  • 从 S3 获取映像

重要

第三方服务和推送通知弃用

App Services 中的第三方服务和推送通知已弃用,转而创建在函数中使用外部依赖项的 HTTP 端点。

Webhook 已重命名为 HTTPS 端点,行为没有发生变化。您应该迁移现有的 Webhook。

现有服务将继续运行到 2024 年 11 月 1 日

由于第三方服务和推送通知现已弃用,因此,默认将其从 App Services 用户界面中删除。如果您需要管理现有的第三方服务或推送通知,可以执行以下操作以将配置重新添加到用户界面中:

  • 在左侧导航栏中的 Manage(管理)部分下面,单击 App Settings(应用设置)。

  • 启用 Temporarily Re-Enable 3rd Party Services(暂时重新启用第三方服务)旁边的切换开关,然后保存更改。

本页上的代码片段演示了如何通过Amazon Web Services Service 使用 Amazon Simple Storage Service 。所有代码段都需要一个 Amazon Web Services 服务接口,其中配置了允许在代码段中使用的服务操作的 Amazon Web Services 服务规则。您可以将此代码复制到Realm 函数中,自行运行这些代码片段。

如果您的应用程序没有 Amazon Web Services 服务接口,请在使用这些代码段之前创建一个接口。

Atlas Function 643使用 PutObject 将 Base 编码的图像上传到 AWS S 操作。

exports = function(base64EncodedImage, bucket, fileName, fileType) {
// Convert the base64 encoded image string to a BSON Binary object
const binaryImageData = BSON.Binary.fromBase64(base64EncodedImage, 0);
// Instantiate an S3 service client
const s3Service = context.services.get('myS3Service').s3('us-east-1');
// Put the object to S3
return s3Service.PutObject({
'Bucket': bucket,
'Key': fileName,
'ContentType': fileType,
'Body': binaryImageData
})
.then(putObjectOutput => {
console.log(putObjectOutput);
// putObjectOutput: {
// ETag: <string>, // The object's S3 entity tag
// }
return putObjectOutput
})
.catch(console.error);
};
范围
类型
说明
base64EncodedImage
字符串
Base64 编码的图像。您可以将图像 文件 64使用 readAsDataURL 转换 为基 FileReader Web API 中的方法。
bucket
字符串
将保存图像的 S3 存储桶的名称。
fileName
字符串
图像文件的名称,包括文件扩展名。
fileType
字符串
MIME 类型 的图像。

Atlas Function 使用3 GetObject 从 AWS S 检索对象 操作。

exports = function(bucket, fileName) {
// Instantiate an S3 service client
const s3Service = context.services.get('myS3Service').s3('us-east-1');
// Get the object from S3
return s3Service.GetObject({
'Bucket': bucket,
'Key': fileName,
})
.then(getObjectOutput => {
console.log(getObjectOutput);
// {
// ETag: <string>, // The object's S3 entity tag
// Body: <binary>, // The object data
// ContentType: <string>, // The object's MIME type
// }
const base64EncodedImage = getObjectOutput.Body
return base64EncodedImage
})
.catch(console.error);
};
范围
类型
说明
bucket
字符串
将保存图像的 S3 存储桶的名称。
fileName
字符串
图像文件的名称,包括文件扩展名。
← GitHub 服务 [已弃用]