Code Monkey home page Code Monkey logo

aliyun-oss-laravel's Introduction

English | 简体中文

Aliyun OSS Laravel

Latest Stable Version Total Downloads tests License Coverage Status

Banner

This package is a wrapper bridging aliyun-oss-flysystem into Laravel as an available storage disk.
If client direct transmission is required, Use web server signature direct transmission OSS extension package aliyun-oss-appserver.

Compatibility

laravel aliyun-oss-laravel driver readme
>=5.5,<9.0 ^3.0 aliyun readme
>=9.0 ^4.0 oss readme

Installation

  1. If you use the composer to manage project dependencies, run the following command in your project"s root directory:

    composer require alphasnow/aliyun-oss-laravel

    Then run composer install to install the dependency.

  2. Modify the environment file .env

    OSS_ACCESS_KEY_ID=<Your aliyun accessKeyId, Required>
    OSS_ACCESS_KEY_SECRET=<Your aliyun accessKeySecret, Required>
    OSS_BUCKET=<Your oss bucket name, Required>
    OSS_ENDPOINT=<Your oss endpoint domain, Required>
    1. (Optional) Modify the configuration file config/filesystems.php
      "default" => env("FILESYSTEM_DRIVER", "oss"),
      // ...
      "disks"=>[
          // ...
          "oss" => [
              "driver"            => "oss",
              "access_key_id"     => env("OSS_ACCESS_KEY_ID"),           // Required, yourAccessKeyId
              "access_key_secret" => env("OSS_ACCESS_KEY_SECRET"),       // Required, yourAccessKeySecret
              "bucket"            => env("OSS_BUCKET"),                  // Required, for example: my-bucket
              "endpoint"          => env("OSS_ENDPOINT"),                // Required, for example: oss-cn-shanghai.aliyuncs.com
              "internal"          => env("OSS_INTERNAL", null),          // Optional, for example: oss-cn-shanghai-internal.aliyuncs.com
              "domain"            => env("OSS_DOMAIN", null),            // Optional, for example: oss.my-domain.com
              "is_cname"          => env("OSS_CNAME", false),            // Optional, if the Endpoint is a custom domain name, this must be true, see: https://github.com/aliyun/aliyun-oss-php-sdk/blob/572d0f8e099e8630ae7139ed3fdedb926c7a760f/src/OSS/OssClient.php#L113C1-L122C78
              "prefix"            => env("OSS_PREFIX", ""),              // Optional, the prefix of the store path
              "use_ssl"           => env("OSS_SSL", false),              // Optional, whether to use HTTPS
              "throw"             => env("OSS_THROW", false),            // Optional, whether to throw an exception that causes an error
              "signatureVersion"  => env("OSS_SIGNATURE_VERSION", "v1"), // Optional, select v1 or v4 as the signature version
              "region"            => env("OSS_REGION", ""),              // Optional, for example: cn-shanghai, used only when v4 signature version is selected
              "options"           => [],                                 // Optional, add global configuration parameters, For example: [\OSS\OssClient::OSS_CHECK_MD5 => false]
              "macros"            => []                                  // Optional, add custom Macro, For example: [\App\Macros\ListBuckets::class, \App\Macros\CreateBucket::class]
          ],
          // ...
      ]

Usage

use Illuminate\Support\Facades\Storage;
$storage = Storage::disk("oss");

Write

Storage::disk("oss")->putFile("dir/path", "/local/path/file.txt");
Storage::disk("oss")->putFileAs("dir/path", "/local/path/file.txt", "file.txt");

Storage::disk("oss")->put("dir/path/file.txt", file_get_contents("/local/path/file.txt"));
$fp = fopen("/local/path/file.txt","r");
Storage::disk("oss")->put("dir/path/file.txt", $fp);
fclose($fp);

Storage::disk("oss")->prepend("dir/path/file.txt", "Prepend Text"); 
Storage::disk("oss")->append("dir/path/file.txt", "Append Text");

Storage::disk("oss")->put("dir/path/secret.txt", "My secret", "private");
Storage::disk("oss")->put("dir/path/download.txt", "Download content", ["headers" => ["Content-Disposition" => "attachment;filename=download.txt"]]);

Read

Storage::disk("oss")->url("dir/path/file.txt");
Storage::disk("oss")->temporaryUrl("dir/path/file.txt", \Carbon\Carbon::now()->addMinutes(30));

Storage::disk("oss")->get("dir/path/file.txt"); 

Storage::disk("oss")->exists("dir/path/file.txt"); 
Storage::disk("oss")->size("dir/path/file.txt"); 
Storage::disk("oss")->lastModified("dir/path/file.txt");

Delete

Storage::disk("oss")->delete("dir/path/file.txt");
Storage::disk("oss")->delete(["dir/path/file1.txt", "dir/path/file2.txt"]);

File operation

Storage::disk("oss")->copy("dir/path/file.txt", "dir/path/file_new.txt");
Storage::disk("oss")->move("dir/path/file.txt", "dir/path/file_new.txt");
Storage::disk("oss")->rename("dir/path/file.txt", "dir/path/file_new.txt");

Folder operation

Storage::disk("oss")->makeDirectory("dir/path"); 
Storage::disk("oss")->deleteDirectory("dir/path");

Storage::disk("oss")->files("dir/path");
Storage::disk("oss")->allFiles("dir/path");

Storage::disk("oss")->directories("dir/path"); 
Storage::disk("oss")->allDirectories("dir/path"); 

Use Macro

Storage::disk("oss")->appendObject("dir/path/news.txt", "The first line paragraph.", 0);
Storage::disk("oss")->appendObject("dir/path/news.txt", "The second line paragraph.", 25);
Storage::disk("oss")->appendObject("dir/path/news.txt", "The last line paragraph.", 51);

Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.001", 0);
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.002", 1024);
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.003", 1024);
Add Custom Macro
  1. Add Macro

    namespace App\Macros;
    use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
    
    class ListBuckets implements AliyunMacro
    {
        // ... 
    }

    Reference code: AppendObject.php

  2. Modify the config

    [
        "macros" => [\App\Macros\ListBuckets::class]
    ]
  3. Use Macro

    Storage::disk("oss")->listBuckets()

Use OssClient

use AlphaSnow\LaravelFilesystem\Aliyun\OssClientAdapter;

$adapter = new OssClientAdapter(Storage::disk("oss"));
$adapter->client()->appendObject($adapter->bucket(), $adapter->path("dir/path/file.txt"), "contents", 0, $adapter->options(["visibility" => "private"]));

Documentation

AlibabaCloud Object Storage Service Documentation

Issues

Opening an Issue

Contributors

Star History

Star History Chart

License

MIT

aliyun-oss-laravel's People

Contributors

alphasnow avatar aspirantzhang avatar fossabot avatar woshizhazha120 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

aliyun-oss-laravel's Issues

php8 报错了复现了这个issue的问题

没有复现上述错误,初步判断可能你的配置有问题,可以尝试将以下修改.
The error was not repeated, May be that you have a configuration problem, Try the following modifications.

ALIYUN_OSS_ENDPOINT=<change it to your own oss endpoint>
ALIYUN_OSS_IS_CNAME=false

Originally posted by @alphasnow in #1 (comment)

OSS上传报错

php版本:PHP 7.4.19 (cli)
laravel版本:8.47.0
aliyun-oss-laravel版本:2.8.0

错误信息返回:

exception: "OSS\\Core\\OssException"
file: "~/admin/vendor/aliyuncs/oss-sdk-php/src/OSS/Result/Result.php"
line: 97
message: ":  RequestId: 60CAB8775423BA3639E2504A"

错误信息很不明确啊,能不能直接抛出来阿里云oss的异常id和message,方便查找问题

Laravel 10好像有问题

运行环境 / Runtime environment

  • php: 8.1
  • laravel: 10
  • aliyun-oss-laravel: 4.7

描述问题 / Describe the bug

Driver [oss] is not supported.

unable to check existence for laravel9

运行环境 / Runtime environment

  • php: 8.0.2
  • laravel: 9.33.0
  • aliyun-oss-laravel: 2.6.0

描述问题 / Describe the bug

[2023-03-27 09:45:53] local.ERROR: Unable to check existence for: images/305913b61cfb266b8140cc8a2ba813c4.png {"exception":"[object] (League\\Flysystem\\UnableToCheckExistence(code: 0): Unable to check existence for: images/305913b61cfb266b8140cc8a2ba813c4.png vendor/league/flysystem/src/UnableToCheckExistence.php:19)
[stacktrace]

laravel8上传失败,返回false

运行环境 / Runtime environment

  • php: 7.4
  • laravel: 8.75
  • aliyun-oss-laravel: 3.3

描述问题 / Describe the bug

使用文档给的所有方式都上传不到oss

配置文件:

's3' => [
  'driver' => 's3',
  'key' => env('AWS_ACCESS_KEY_ID'),
  'secret' => env('AWS_SECRET_ACCESS_KEY'),
  'region' => env('AWS_DEFAULT_REGION'),
  'bucket' => env('AWS_BUCKET'),
  'url' => env('AWS_URL'),
  'endpoint' => env('AWS_ENDPOINT'),
  'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],

"aliyun" => [
  "driver"     => "aliyun",
  "access_id"  => env("ALIYUN_OSS_ACCESS_ID"),  
  "access_key" => env("ALIYUN_OSS_ACCESS_KEY"),  
  "bucket"     => env("ALIYUN_OSS_BUCKET"),    
  "endpoint"   => env("ALIYUN_OSS_ENDPOINT"), 
  "internal"   => env("ALIYUN_OSS_INTERNAL", null),
  "domain"     => env("ALIYUN_OSS_DOMAIN", null),  
  "use_ssl"    => env("ALIYUN_OSS_USE_SSL", false), 
  "prefix"     => env("ALIYUN_OSS_PREFIX", null), 
  "throw"      => true,
],

env里面有对应的配置

$extension = $file->getClientOriginalExtension();
$filename = Uuid::uuid4()->getHex() . '.' . $extension;
//$p = Storage::disk('aliyun')->putFileAs('uploads/abc', $file, $filename); // 打印了false
$p = Storage::disk('aliyun')->putFile('uploads/abc', $file); // 打印了false

dd($p);

执行代码打印的是false
如果将aliyun驱动换成local驱动的话就上传成功了

我还同时安装了这个包”alphasnow/aliyun-oss-appserver“,sts是可以获取到也是有效的

麻烦看看是什么原因

CDN 在哪里配置

运行环境 / Runtime environment

  • php: 8.1.5
  • laravel: ^9.1
  • aliyun-oss-laravel: ^4.7

这些参数哪一个是cdn的域名配置

期望配置cdn后,签名的url返回的是cdn的域名

return [
    "driver" => "oss",
    "access_key_id" => env("OSS_ACCESS_KEY_ID"), // Required, YourAccessKeyId
    "access_key_secret" => env("OSS_ACCESS_KEY_SECRET"), // Required, YourAccessKeySecret
    "endpoint" => env("OSS_ENDPOINT"), // Required, Endpoint
    "bucket" => env("OSS_BUCKET"), // Required, Bucket
    "prefix" => env("OSS_PREFIX", ""), // For example: user/uploads
    "request_proxy" => env("OSS_PROXY", null), // Used by \OSS\OssClient
    "security_token" => env("OSS_TOKEN", null), // Used by \OSS\OssClient
    "is_cname" => env("OSS_CNAME", false), // If this is the CName and binded in the bucket.
    "use_ssl" => env("OSS_SSL", null), // Whether to use HTTPS
    "max_retries" => env("OSS_MAX_TRIES", null), // Sets the max retry count
    "timeout" => env("OSS_TIMEOUT", null), // The request timeout time
    "connect_timeout" => env("OSS_CONNECT_TIMEOUT", null), // The connection timeout time
    "enable_sts_in_url" => env("OSS_STS_URL", null), // Enable/disable STS in the URL
    "internal" => env("OSS_INTERNAL", null), // For example: oss-cn-shanghai-internal.aliyuncs.com
    "domain" => env("OSS_DOMAIN", null), // For example: oss.my-domain.com
    "reverse_proxy" => env("OSS_REVERSE_PROXY", false), // Whether to use the Reverse proxy, such as nginx
    "options" => [], // For example: [\OSS\OssClient::OSS_CHECK_MD5 => false]
    "macros" => [] // For example: [\App\Macros\ListBuckets::class,\App\Macros\CreateBucket::class]
];

是否可以移除 laravel 版本的依赖

运行环境 / Runtime environment

  • php: 8
  • laravel: 9
  • aliyun-oss-laravel: 4.x

描述问题 / Describe the bug

4.x 增加了对 laravel framework 的依赖,是否可以移除,或者仅依赖 illuminate/filesystem:^9.0
这样在非完整的 laravel 项目下也可以直接依赖这个包,而非 alphasnow/aliyun-oss-flysystem

PHP Fatal error: Access level to AlphaSnow\AliyunOss\Adapter::getOptionsFromConfig() must be public

i am using Laravel 8.52 with php 8.0. The package I use is alphasnow/aliyun-oss-laravel version 3.0

When I try on tinker with following command
Storage::put('a.txt', 'Test123')

got the following error
PHP Fatal error: Access level to AlphaSnow\AliyunOss\Adapter::getOptionsFromConfig() must be public (as in class AlphaSnow\Flysystem\AliyunOss\AliyunOssAdapter) in /var/www/html/project/vendor/alphasnow/aliyun-oss-laravel/src/Adapter.php on line 31

OSS_DOMAIN

When we add the OSS_DOMAIN=mybucketdomain.com

when i use Storage::exists(filename.jpg)
it will give error Unable to check existence for: filename.jpg. RequestCoreException: cURL error: Could not resolve host: bucketname.mybucketdomain.com (6)

When there is a prefix, the path is spliced ​​2 times

运行环境 / Runtime environment

  • php: 8.0.2
  • laravel: 9.19
  • aliyun-oss-laravel: 4.6

描述问题 / Describe the bug

The url method returns the prefix twice

   # prefix=test
    $disk = Storage::disk('oss');
    $filePath = "20221114/1.jpg";
    return $disk->url($filePath);  # return http://{url}/test/test/20221114/1.jpg

设置header无效

问题已发现,并给出了解决方案,不知道是否合理~

我的环境:
"php": "^7.2",
"laravel/framework": "^6.0",
"alphasnow/aliyun-oss-laravel": "^2.7",

问题:
头设置Content-Disposition无效,windows可以,linux不行~

// 这样不行
config(['filesystems.disks.aliyun.options' => [
    OssClient::OSS_HEADERS => [
        'Content-Disposition' => 'attachment; filename="ceshi456.xlsx"',
    ]
]]);
$storage = Storage::disk('aliyun')->putFileAs('/', 'test123.xlsx', 'ceshi456.xlsx');

// 这样不行
$options = [
    OssClient::OSS_HEADERS => [
        'Content-Disposition' => 'attachment; filename="yadgen_test.xlsx"',
    ]
];
$storage = Storage::disk('aliyun')->putFileAs('/', 'test123.xlsx', 'ceshi456.xlsx', $options);

解决:

// 调用时
$options = [
    OssClient::OSS_HEADERS => [
        'Content-Disposition' => 'attachment; filename="yadgen测试.xlsx"',
    ],
];

// 修改文件:alphasnow/aliyun-oss-flysystem/src/AliyunOssAdapter.php
// 方法:getOptionsFromConfig()替换成如下:
protected function getOptionsFromConfig(Config $config)
{
    $options = [];

    if ($visibility = $config->get("visibility")) {
        $options[OssClient::OSS_HEADERS][OssClient::OSS_OBJECT_ACL] = $this->visibilityToAcl($visibility);
    }

    $options[OssClient::OSS_HEADERS] = $config->get('headers');

    return $options;
}

The upload does not work

  • php: 8.1
  • laravel: 9
  • aliyun-oss-laravel: 4.7

I create a user that has full OSS permission

I create a bucket read/write

I install your pkg and put all credentials in .env

OSS_ACCESS_KEY_ID=LTAI5t7LT1XXXXXX
OSS_SECRET_ACCESS_KEY=902sCyeJgQXXXXXXXX
OSS_BUCKET=XXXX
OSS_ENDPOINT=https://XXXXXaliyuncs.com

I use this code too upload a test file:

Route::get('oss', function () {
    dd(Storage::disk('oss')->put('test.txt', 'Hello World'));
});

I got a false in response!

Am I missing something?

Driver [oss] is not supported

运行环境 / Runtime environment

  • php: 7.4.33
  • laravel: 8.12
  • aliyun-oss-laravel: 3.3

描述问题 / Describe the bug

composer require 跟 install 後, 要使用時出現 Driver [oss] is not supported.

在 app.php 的 providers 加上 AlphaSnow\LaravelFilesystem\Aliyun\AliyunServiceProvider::class 會出錯,找不到此檔案
改成加上 AlphaSnow\AliyunOss\ServiceProvider::class 一樣出現 Driver [oss] is not supported.

前端直传

您好, 请问有前端直传的相关配置吗

文档错误

文档写的是 "driver" => "oss",
看文件。AlphaSnow\AliyunOss\ServiceProvider 实际是driver" => "aliyun",
access_key_id 应该是access_id
access_key_secret应该是 access_key

laravel8 不支持

laravel8.12 不支持,

"alphasnow/aliyun-oss-laravel": "^2.5",

$filename = md5(microtime()) . '.png'; $rs = Storage::disk('aliyun')->exists('WechatIMG50.png');

直接报错
OSS\Core\OssException

: RequestId:

at vendor/aliyuncs/oss-sdk-php/src/OSS/Result/Result.php:97
93▕ 'code' => $code,
94▕ 'message' => $message,
95▕ 'body' => $body
96▕ );
➜ 97▕ throw new OssException($details);
98▕ }
99▕ }
100▕
101▕ /**

  +5 vendor frames 

6 app/Console/Commands/TestCommand.php:121
Illuminate\Filesystem\FilesystemAdapter::exists("WechatIMG50.png")

  +13 vendor frames 

20 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.