将文件迁移到AWS

时间:2016-06-26 03:57:13

标签: amazon-web-services parse-platform amazon-s3

有没有人成功将文件从Parse S3 Bucket迁移到自己的S3 Bucket?我有一个包含许多文件(图像)的应用程序,我使用S3文件适配器从我自己的S3 Bucket和Parse Bucket服务但是想将物理文件迁移到我自己的应用程序将在AWS上的Bucket现在被托管。

提前致谢!

1 个答案:

答案 0 :(得分:2)

如果您已将新的Parse实例配置为使用S3文件适配器托管文件,则可以编写一个PHP脚本,从Parse S3 Bucket下载文件并将其上载到您自己的文件中。在我的示例中(使用Parse-PHP-SDK):

  1. 我在每个条目中循环。
  2. 我下载了该文件的二进制文件(托管在Parse中)
  3. 我将其作为新ParseFile上传(如果您的服务器配置为S3,它将上传到您自己的S3存储桶中。)
  4. 将新ParseFile应用于您的参赛作品。
  5.     <?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!";
    
        ?>