Laravel:如何在不下载其他库的情况下通过SFTP传输文件到Azure Blob存储?

发布于 2025-02-13 17:15:58 字数 404 浏览 1 评论 0 原文

我需要能够在Azure Blob存储上的某个目录上传输文件,列表文件和删除文件,并使用现有的Laravel磁盘命令,例如 put> put() allfiles(Allfiles(),<代码> delete()。

以下是我上传文件的代码:

use Storage;

class SFTPFileUploader
{
    public function uploadFileToAzure($fileName,$content)
    {
        $sftpAzureDisk= Storage::disk('sftp');
        $sftpAzureDisk->put($fileName,$content);
    }
}

I need to be able to transfer files, list files and delete files on a certain directory on my Azure Blob Storage and use existing Laravel Disk commands like put(), allFiles(), delete().

Below is my code for uploading a file:

use Storage;

class SFTPFileUploader
{
    public function uploadFileToAzure($fileName,$content)
    {
        $sftpAzureDisk= Storage::disk('sftp');
        $sftpAzureDisk->put($fileName,$content);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2025-02-20 17:15:58

为了能够执行此操作而无需下载其他库,

首先,您需要确保

1. Azure Storage Account (e.g myprojectstorage)
2. Azure Storage Container (e.g myprojectcontainerfolder)
3. Azure Storage Account -> Settings -> SFTP
      3.1 Create a Local User (If none yet)
          3.1.1 Create User Name
          3.1.2 Generate Password
          3.1.3 Set Permissions
      3.2 Enable SFTP

在设置所有这些之后,在Azure Portal中具有以下设置,您可以继续进行 config> config/filesystems.php 并创建新连接

'sftp' => [
            'driver' => 'sftp',
            'host' => "<myprojectstorage>.blob.core.windows.net",
            'port' => 22,
            'username' => "<myprojectstorage>.<myprojectcontainer>.<username>",
            'password' => <password>,
            'privateKey' => storage_path('app/public/your.key'),//optional depends on Azure Setup
            'root' => '/',
        ],

,然后这应该已经可以工作了,而无需下载任何其他库,

use Storage;

class SFTPFileUploader
{
    public function uploadFileToAzure($fileName,$content)
    {
        $sftpAzureDisk= Storage::disk('sftp');
        $sftpAzureDisk->put($fileName,$content);
    }
}

PS(我只使用密码进行了测试,而未对密钥文件进行测试)

To be able to do this without having to download other libraries,

First you need make sure you have the following setup in your Azure Portal

1. Azure Storage Account (e.g myprojectstorage)
2. Azure Storage Container (e.g myprojectcontainerfolder)
3. Azure Storage Account -> Settings -> SFTP
      3.1 Create a Local User (If none yet)
          3.1.1 Create User Name
          3.1.2 Generate Password
          3.1.3 Set Permissions
      3.2 Enable SFTP

After setting all of those, you can proceed to your config/filesystems.php and create new connection

'sftp' => [
            'driver' => 'sftp',
            'host' => "<myprojectstorage>.blob.core.windows.net",
            'port' => 22,
            'username' => "<myprojectstorage>.<myprojectcontainer>.<username>",
            'password' => <password>,
            'privateKey' => storage_path('app/public/your.key'),//optional depends on Azure Setup
            'root' => '/',
        ],

then this should already work without having to download any other library,

use Storage;

class SFTPFileUploader
{
    public function uploadFileToAzure($fileName,$content)
    {
        $sftpAzureDisk= Storage::disk('sftp');
        $sftpAzureDisk->put($fileName,$content);
    }
}

P.S (I only have tested working using with Password, not tested yet for Key File)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文