簡體   English   中英

如何使用 Magento 2 中的 sftp 將文件從一台服務器傳輸到另一台服務器?

[英]How to transfer file from one server to another using sftp in Magento 2?

我想使用 sftp 將與該產品相關的所有圖像從 M1 傳輸到 M2。 我可以使用 sftp 連接到 M1。 但我不知道如何轉移它。

這是與 sftp 連接的代碼 -

//FTP Connection
public function connectFtp($host, $user, $password, $ssl=true, $passive=true){
        return $connect = $this->sftp->open(
            array(
                'host'      =>  $host,
                'user'      =>  $user,
                'password'  =>  $password,
                'ssl'       =>  $ssl,
                'passive'   =>  $passive
            )
        );
    }

//Downlaod images from M1 and transfer to M2 temp folder
    public function downloadImages($images){
        //Connecting to M1
        $connect = $this->connectFtp(SELF::M1_HOST, SELF::M1_USERNAME, SELF::M1_PASSWORD, SELF::M1_SSL, SELF::M1_PASSIVE);
        if($connect){
           /* Code to transfer */
        }
    }

如何實現這種文件傳輸? 根據一些項目要求,我們不想使用任何插件。

您是否只想將所有圖像從 M1 傳輸到 M2?

您可以嘗試將圖像重新同步:

rsync user@m1.host:/path/to/copy user@m2.host:/path/to/copy

從 SFTP 服務器到我們的服務器的文件傳輸的工作代碼。

  $sftp = $obj->create('Magento\Framework\Filesystem\Io\Sftp');
  $open =  $sftp->open(
       array(
           'host' => $sftp_hostname,
           'username' => $sftp_username,
           'password' => $sftp_password,
           'port'=>$sftp_port
       )
    );

    try{
        // Change directory and move to require directory
        $sftp->cd('/enter_your_sftp_file_path_here/');

        //Fetching/Listing all the files.
        $sftp_server_files = $sftp->ls();

        //File path to put files from SFTP Server to our local server
        $filepath = $dir->getRoot() . '/var/import/inventory/';

        foreach ($sftp_server_files as $file) {

            //Copy files from server
            $source = $file['text'];
            if(strpos($source, 'zip') !== false){
                $destination = $filepath.$source;
                $result = $sftp->read($source, $destination);
                if($result == 'true') {
                   $message[] = 'File read from SFTP server : '.$source;
                }
                else
                {
                  $message[] = 'File not able to read from SFTP server : '.$source;
                }
            }
        }
    } catch ( Exception $e) {
        echo $e->getMessage();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM