You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel如何无需额外下载库通过SFTP传输文件到Azure Blob Storage

Laravel 对接 Azure Blob Storage 实现指定目录下的文件操作

直接通过Laravel内置Storage扩展机制对接,配置完成后可无差别使用put()、allFiles()、delete()等所有原生Disk方法,所有操作默认限定在你指定的目录下,无需手动拼接路径。

步骤1:安装依赖

执行composer命令安装Azure Blob对应的Flysystem适配包:

composer require league/flysystem-azure-blob-storage

步骤2:添加磁盘配置

打开config/filesystems.php,在disks数组中新增azure磁盘配置,通过root参数指定你要操作的目标目录:

'disks' => [
    // 保留原有s3、local、sftp等磁盘配置不变
    'azure' => [
        'driver' => 'azure-blob',
        'connection_string' => env('AZURE_STORAGE_CONNECTION_STRING'),
        'container' => env('AZURE_STORAGE_CONTAINER'),
        // 填写需要限定操作的根目录,所有文件操作都会默认在该目录下执行
        'root' => env('AZURE_BLOB_ROOT_PATH', ''),
    ],
],

步骤3:注册自定义存储驱动

打开app/Providers/AppServiceProvider.php,在boot方法中注册azure-blob驱动:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter;
use League\Flysystem\Filesystem;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Storage::extend('azure-blob', function ($app, $config) {
            $client = BlobRestProxy::createBlobService($config['connection_string']);
            $adapter = new AzureBlobStorageAdapter(
                $client,
                $config['container'],
                $config['root'] ?? ''
            );
            return new Filesystem($adapter);
        });
    }
}

步骤4:填写环境变量

在项目.env文件中添加Azure存储对应的配置项:

AZURE_STORAGE_CONNECTION_STRING=你的Azure Blob存储账号连接字符串
AZURE_STORAGE_CONTAINER=存储容器名称
AZURE_BLOB_ROOT_PATH=你要限定操作的指定目录,例如 biz/user-upload/2024

常用操作示例

配置完成后直接调用Laravel原生Storage方法即可,你之前写的上传逻辑只需要把磁盘名从sftp替换为azure就能正常运行,以下是覆盖你需求的完整操作示例:

<?php

namespace App\Utils;

use Illuminate\Support\Facades\Storage;

class AzureFileOperator
{
    protected $disk;

    public function __construct()
    {
        $this->disk = Storage::disk('azure');
    }

    /**
     * 上传文件(文件传输写入)
     * @param string $fileName 相对于根目录的文件路径
     * @param string|resource $content 文件内容/文件流
     * @return bool
     */
    public function upload(string $fileName, $content): bool
    {
        return $this->disk->put($fileName, $content);
    }

    /**
     * 列举目录下所有文件
     * @param string|null $dir 子目录路径,不传默认列举根目录所有文件
     * @return array 文件路径列表
     */
    public function listFiles(?string $dir = null): array
    {
        return $this->disk->allFiles($dir);
    }

    /**
     * 删除指定文件
     * @param string|array $filePath 单个文件路径/多个文件路径数组
     * @return bool
     */
    public function delete($filePath): bool
    {
        return $this->disk->delete($filePath);
    }

    /**
     * 读取文件内容(文件传输下载)
     * @param string $filePath
     * @return string
     */
    public function getContent(string $filePath): string
    {
        return $this->disk->get($filePath);
    }
}

注意:所有传入方法的路径均为相对于配置中root目录的相对路径,不需要手动拼接根目录前缀,Laravel会自动完成路径映射,除了以上示例方法,exists()、copy()、move()、size()、lastModified()等所有Storage原生方法都可以直接调用,行为和本地磁盘完全一致。

内容的提问来源于stack exchange,提问作者apelidoko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 16:57:18