PHP code example of dtyq / cloudfile
1. Go to this page and download the library: Download dtyq/cloudfile library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dtyq / cloudfile example snippets
$configs = [
'storages' => [
// 文件服务配置示例
'file_service_test' => [
'adapter' => 'file_service',
'config' => [
// 文件服务地址
'host' => 'xxx',
// 文件服务的 platform
'platform' => 'xxx',
// 文件服务的 key
'key' => 'xxx',
],
],
// 阿里云配置示例
'aliyun_test' => [
'adapter' => 'aliyun',
'config' => [
'accessId' => 'xxx',
'accessSecret' => 'xxx',
'bucket' => 'xxx',
'endpoint' => 'xxx',
'role_arn' => 'xxx',
],
],
// 火山云配置示例
'tos_test' => [
'adapter' => 'tos',
'config' => [
'region' => 'xxx',
'endpoint' => 'xxx',
'ak' => 'xxx',
'sk' => 'xxx',
'bucket' => 'xxx',
'trn' => 'xxx',
],
],
// MinIO 直连配置示例(底层复用 S3 协议实现)
'minio_test' => [
'adapter' => 'minio',
'config' => [
'region' => 'us-east-1',
'endpoint' => 'http://localhost:9000', // MinIO 服务地址
'accessKey' => 'xxx',
'secretKey' => 'xxx',
'bucket' => 'xxx',
'use_path_style_endpoint' => true, // MinIO 必须为 true
'version' => 'latest',
'role_arn' => 'xxx', // 可选,用于 STS 临时凭证
],
],
],
];
$container = new SdkContainer([
// sdk 基本配置
'sdk_name' => 'easy_file_sdk',
'exception_class' => CloudFileException::class,·
// cloudfile 配置
'cloudfile' => $configs,
]);
$cloudFile = new CloudFile($container);
$filesystem = $cloudFile->get('file_service_test');
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
'cache' => false, // 根据需要设置,建议 false,方便调试
];
$filesystem = $cloudFile->get('file_service_test');
$credentialPolicy = new CredentialPolicy([
'sts' => false,
'roleSessionName' => 'test',
]);
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
];
$data = $filesystem->getUploadTemporaryCredential($credentialPolicy, $options);
$filesystem = $cloudFile->get('file_service_test');
$credentialPolicy = new CredentialPolicy([
'sts' => false,
]);
$realPath = __DIR__ . '/../test.txt';
$uploadFile = new UploadFile($realPath, 'easy-file');
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
];
$filesystem->uploadByCredential($uploadFile, $credentialPolicy, $options);
$filesystem = $cloudFile->get('file_service_test');
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
];
// 复制文件成功后,要获取这个 path 结果才是真实地址,因为文件服务会有权限处理
$path = $filesystem->duplicate('easy-file/test.txt', 'easy-file/test-copy.txt', $options);
$filesystem = $cloudFile->get('file_service_test');
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
];
$filesystem->destroy('easy-file/test.txt', $options);
$filesystem = $cloudFile->get('file_service_test');
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
];
$list = $filesystem->getLinks([
'easy-file/file-service.txt',
'easy-file/test.txt',
], [], 7200, $options);
$filesystem = $cloudFile->get('file_service_test');
$options = [
'token' => 'xxx',
'organization-code' => 'xxx',
];
$list = $filesystem->getMetas([
'easy-file/file-service.txt',
'easy-file/test.txt'], $options);
// 这里可以在构造中注入 CloudFileFactory
$cloudFile = \Hyperf\Support\make(CloudFileFactory::class)->create();
$filesystem = $cloudFile->get('file_service');
$options = [
// 这里的动态 token 需要自行传入
'token' => 'xxx',
'organization-code' => 'xxx',
];
$list = $filesystem->getLinks([
'easy-file/file-service.txt',
'easy-file/test.txt',
], [], 7200, $options);
$link = $list[0]->getUrl();
// 在 .env 中配置
MINIO_ENDPOINT=http://localhost:9000
MINIO_REGION=us-east-1
MINIO_ACCESS_KEY=your-access-key
MINIO_SECRET_KEY=your-secret-key
MINIO_BUCKET=your-bucket
// 在代码中使用
$cloudFile = \Hyperf\Support\make(CloudFileFactory::class)->create();
$filesystem = $cloudFile->get('minio');
// 上传文件
$uploadFile = new UploadFile('/path/to/file.txt', 'my-folder');
$credentialPolicy = new CredentialPolicy(['sts' => false]);
$filesystem->uploadByCredential($uploadFile, $credentialPolicy);
// 获取文件链接
$links = $filesystem->getLinks(['my-folder/file.txt'], [], 3600);
$filesystem = $cloudFile->get('minio_test');
// 获取临时凭证
$credentialPolicy = new CredentialPolicy([
'sts' => false, // 返回 MinIO 表单直传 credential,字段语义与 file_service/minio 保持一致
'roleSessionName' => 'test',
]);
$credential = $filesystem->getUploadTemporaryCredential($credentialPolicy);
// 上传文件
$realPath = __DIR__ . '/test.txt';
$uploadFile = new UploadFile($realPath, 'my-folder');
$filesystem->uploadByCredential($uploadFile, $credentialPolicy);
// 分片上传 / 预签名 URL 等 SDK 场景需要 STS 凭证
$stsPolicy = new CredentialPolicy([
'sts' => true,
'roleSessionName' => 'test',
]);
$stsCredential = $filesystem->getUploadTemporaryCredential($stsPolicy);
// 获取文件链接
$links = $filesystem->getLinks(['my-folder/test.txt'], [], 3600);
$downloadUrl = $links[0]->getUrl();
// 复制文件
$newPath = $filesystem->duplicate('my-folder/test.txt', 'my-folder/test-copy.txt');
// 删除文件
$filesystem->destroy('my-folder/test.txt');
shell
$ php bin/hyperf.php vendor:publish dtyq/cloudfile