deleteObjects($bucket, $object);" and other methods."/> deleteObjects($bucket, $object);" and other methods.">

Home  >  Article  >  Backend Development  >  How to delete files in php oss

How to delete files in php oss

藏色散人
藏色散人Original
2020-10-22 09:28:243828browse

php How to delete files in oss: first download and install the sdk in Alibaba Cloud; then delete the files through methods such as "$ossClient->deleteObjects($bucket, $object);".

How to delete files in php oss

Recommended: "PHP Video Tutorial"

oss file upload and deletion (batch deletion) processing

The blogger uses Alibaba Cloud's oss

First download and install the SDK on Alibaba Cloud. Please download the relevant SDK from Alibaba Cloud by yourself

Document address https://help.aliyun.com/document_detail/85580.html?spm=a2c4g.11174283.6.1006.55ad7da2hNKC0w#h2-url-2

The blogger used the third method

<?php
require_once &#39;./aliyun-oss-php-sdk-master/autoload.php&#39;;
header("content-type:text/html;charset=utf-8");
// if (is_file(__DIR__ . &#39;/aliyun-oss-php-sdk-master/autoload.php&#39;)) {
//     require_once __DIR__ . &#39;/aliyun-oss-php-sdk-master/autoload.php&#39;;
// }
if (is_file(__DIR__ . &#39;/aliyun-oss-php-sdk-master/autoload.php&#39;)) {
    require_once __DIR__ . &#39;/aliyun-oss-php-sdk-master/autoload.php&#39;;
}
if (is_file(__DIR__ . &#39;/aliyun-oss-php-sdk-master/vendor/autoload.php&#39;)) {
    require_once __DIR__ . &#39;/aliyun-oss-php-sdk-master/vendor/autoload.php&#39;;
}
use OSS\OssClient;
use OSS\Core\OssException;
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
$accessKeyId = "xxxxx";
$accessKeySecret = "xxxxx";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
// 存储空间名称
$bucket = "xxxx";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
//    判断bucketname是否存在,不存在就去创建
if (!$ossClient->doesBucketExist($bucket)) {
    $ossClient->createBucket($bucket);
}
// 文件名称
$object = $_FILES[&#39;filename&#39;][&#39;name&#39;];
// <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt
$filePath = $_FILES[&#39;filename&#39;][&#39;tmp_name&#39;];
try{
    //上传图片
    $ossClient->uploadFile($bucket, $object, $filePath);
    //文件访问权限,设置为所有人可读
    $acl = "public-read";
    $ossClient->putObjectAcl($bucket, $object, $acl);
    echo "<img src =&#39;http://xxxx.oss-cn-shenzhen.aliyuncs.com/".$object."&#39; >";die;
//删除单个文件
//    $ossClient->deleteObjects($bucket, $object);
    //删除多个文件
//    $objects = [&#39;文件名1&#39;,&#39;文件名2&#39;];
//    $ossClient->deleteObjects($bucket, $objects);
    
    } catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK" . "\n");
rrree

The above is the detailed content of How to delete files in php oss. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn