解析 S3FileAdapter
Posted
技术标签:
【中文标题】解析 S3FileAdapter【英文标题】:Parse S3FileAdapter 【发布时间】:2016-04-10 23:08:32 【问题描述】:我目前有一个解析服务器部署工作,但是每次我上传一个文件时,它都会使用GridStore
文件适配器而不是 S3 来存储它。我的应用 JS 如下所示:
// Example express application adding the parse-server module to expose Parse
// compatible API routes.
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var S3Adapter = require('parse-server').S3Adapter;
var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI;
if (!databaseUri)
console.log('DATABASE_URI not specified, falling back to localhost.');
var api = new ParseServer(
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'somAppId',
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
liveQuery:
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
,
filesAdapter: new S3Adapter(
"S31",
"S32",
"bucket-name",
directAccess: true
)
);
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
var app = express();
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res)
res.status(200).send('Make sure to star the parse-server repo on GitHub!');
);
// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res)
res.sendFile(path.join(__dirname, '/public/test.html'));
);
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function()
console.log('parse-server-example running on port ' + port + '.');
);
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
部署后,我没有收到任何错误。一切似乎都运行良好。但是,当我上传文件时,它会转到我的 monglab 数据库而不是我的 S3 存储。我需要做其他事情来配置它吗?我使用文档作为参考来配置它:
https://github.com/ParsePlatform/parse-server/wiki/Configuring-File-Adapters
我还注意到,在解析服务器 repo 中它没有 S3FileAdapter: https://github.com/ParsePlatform/parse-server/tree/master/src/Adapters/Files
任何帮助将不胜感激!
谢谢,
大卫
【问题讨论】:
嗨大卫,我也有同样的问题。你找到解决办法了吗? 【参考方案1】:mLab db 不存储文件。它只存储文件的链接。您需要先将文件上传到 S3。如果成功,Parse 服务器将返回一个文件名,然后将该名称写入 db。我使用 curl 命令来检查文件写入。如果有效,您的设置应该没问题。
curl -X POST \
-H "X-Parse-Application-Id: YOUR-APP-ID" \
-H "X-Parse-REST-API-Key: YOUR-KEY" \
-H "Content-Type: text/plain" \
-d 'Hello, it is me!' \
http://your-parse-server/parse/files/filename.txt
【讨论】:
【参考方案2】:我正在使用这个脚本,它工作正常
filesAdapter: new S3Adapter(
"ACCESS_KEY",
"SECRET_KEY",
"BUCKET_NAME",
"REGION"
)
【讨论】:
【参考方案3】:面对完全相同的问题,在parse-server/lib/Adapters/AdapterLoader.js
中通过时,我发现filesAdapter
允许具有配置选项或功能的自定义模块。所以我尝试了以下对我来说很好的解决方案。
var S3Adapter = require('parse-server').S3Adapter;
var api = new ParseServer(
...
filesAdapter: () =>
return new S3Adapter("ACCESS_KEY", "SECRET_KEY", "BUCKET_NAME")
...
);
【讨论】:
以上是关于解析 S3FileAdapter的主要内容,如果未能解决你的问题,请参考以下文章