【发布时间】:2016-10-28 08:28:46
【问题描述】:
有没有人成功地将文件从 Parse S3 存储桶迁移到自己的 S3 存储桶?我有一个包含许多文件(图像)的应用程序,我让它们从我自己的 S3 存储桶和使用 S3 文件适配器的 Parse 存储桶提供服务,但想将物理文件迁移到我自己在 AWS 上的存储桶,应用程序将现在被托管。
提前致谢!
【问题讨论】:
标签: amazon-web-services parse-platform amazon-s3
有没有人成功地将文件从 Parse S3 存储桶迁移到自己的 S3 存储桶?我有一个包含许多文件(图像)的应用程序,我让它们从我自己的 S3 存储桶和使用 S3 文件适配器的 Parse 存储桶提供服务,但想将物理文件迁移到我自己在 AWS 上的存储桶,应用程序将现在被托管。
提前致谢!
【问题讨论】:
标签: amazon-web-services parse-platform amazon-s3
如果您已将新 Parse 实例配置为使用 S3 文件适配器托管文件,则可以编写一个 PHP 脚本,从 Parse S3 Bucket 下载文件并将其上传到您自己的文件中。在我的例子中(使用Parse-PHP-SDK):
ParseFile 上传(如果您的服务器配置为 S3,它将上传到您自己的 S3 存储桶)。ParseFile 应用于您的条目。瞧
<?php
require 'vendor/autoload.php';
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
use Parse\ParseClient;
$app_id = "AAA";
$rest_key = "BBB";
$master_key = "CCC";
ParseClient::initialize( $app_id, $rest_key, $master_key );
ParseClient::setServerURL('http://localhost:1338/','parse');
$query = new ParseQuery("YourClass");
$query->descending("createdAt"); // just because of my preference
$count = $query->count();
for ($i = 0; $i < $count; $i++) {
try {
$query->skip($i);
// get Entry
$entryWithFile = $query->first();
// get file
$parseFile = $entryWithFile->get("file");
// filename
$fileName = $parseFile->getName();
echo "\nFilename #".$i.": ". $fileName;
echo "\nObjectId: ".$entryWithFile->getObjectId();
// if the file is hosted in Parse, do the job, otherwise continue with the next one
if (strpos($fileName, "tfss-") === false) {
echo "\nThis is already an internal file, skipping...";
continue;
}
$newFileName = str_replace("tfss-", "", $fileName);
$binaryFile = file_get_contents($parseFile->getURL());
// null by default, you don't need to specify if you don't want to.
$fileType = "binary/octet-stream";
$newFile = ParseFile::createFromData($binaryFile, $newFileName, $fileType);
$entryWithFile->set("file", $newFile);
$entryWithFile->save(true);
echo "\nFile saved\n";
} catch (Exception $e) {
// The conection with mongo or the server could be off for some second, let's retry it ;)
$i = $i - 1;
sleep(10);
continue;
}
}
echo "\n";
echo "¡FIN!";
?>
【讨论】: