• 关于

    __autoload()

    的搜索结果

回答

详细解答可以参考官方帮助文档存储空间(Bucket)是对象(Object)的容器,对象必须隶属于某个存储空间。 以下场景的完整代码请参见GitHub。 创建存储空间以下代码用于创建存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 设置存储空间的存储类型为低频访问类型,默认是标准类型。 $options = array( OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA ); // 设置存储空间的权限为公共读,默认是私有读写。 $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储空间的命名规范,请参见基本概念中的命名规范。 判断存储空间是否存在以下代码用于判断指定的存储空间是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $res = $ossClient->doesBucketExist($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}if ($res === true) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");} 列举存储空间以下代码用于列举存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucketListInfo = $ossClient->listBuckets();} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}$bucketList = $bucketListInfo->getBucketList();foreach($bucketList as $bucket) { print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");} 设置存储空间的访问权限存储空间的访问权限(ACL)有以下三类: 访问权限 描述 访问权限值 私有 存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户没有权限操作该存储空间内的文件。 OssClient::OSS_ACL_TYPE_PRIVATE 公共读 存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户只有该存储空间内的文件的读权限。请谨慎使用该权限。 OssClient::OSS_ACL_TYPE_PUBLIC_READ 公共读写 所有用户都有该存储空间内的文件的读写权限。请谨慎使用该权限。 OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE 以下代码用于设置存储空间的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";// 设置存储空间的权限为私有。$acl = OssClient::OSS_ACL_TYPE_PRIVATE;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putBucketAcl($bucket, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取存储空间的访问权限以下代码用于获取存储空间的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $res = $ossClient->getBucketAcl($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");print('acl: ' . $res); 获取存储空间的地域以下代码用于获取存储空间的地域(称为Region或Location): <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Regions = $ossClient->getBucketLocation($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Regions); 关于地域的详细信息请参见基本概念中的地域。 获取存储空间元信息以下代码用于获取存储空间元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Metas = $ossClient->getBucketMeta($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Metas); 删除存储空间删除存储空间之前,必须先删除存储空间下的所有文件、LiveChannel和分片上传产生的碎片。 以下代码用于删除存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteBucket($bucket);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");

2019-12-01 23:14:27 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档存储空间(Bucket)是对象(Object)的容器,对象必须隶属于某个存储空间。 以下场景的完整代码请参见GitHub。 创建存储空间以下代码用于创建存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 设置存储空间的存储类型为低频访问类型,默认是标准类型。 $options = array( OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA ); // 设置存储空间的权限为公共读,默认是私有读写。 $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储空间的命名规范,请参见基本概念中的命名规范。 判断存储空间是否存在以下代码用于判断指定的存储空间是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $res = $ossClient->doesBucketExist($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}if ($res === true) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");} 列举存储空间以下代码用于列举存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucketListInfo = $ossClient->listBuckets();} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}$bucketList = $bucketListInfo->getBucketList();foreach($bucketList as $bucket) { print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");} 设置存储空间的访问权限存储空间的访问权限(ACL)有以下三类: 访问权限 描述 访问权限值 私有 存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户没有权限操作该存储空间内的文件。 OssClient::OSS_ACL_TYPE_PRIVATE 公共读 存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户只有该存储空间内的文件的读权限。请谨慎使用该权限。 OssClient::OSS_ACL_TYPE_PUBLIC_READ 公共读写 所有用户都有该存储空间内的文件的读写权限。请谨慎使用该权限。 OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE 以下代码用于设置存储空间的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";// 设置存储空间的权限为私有。$acl = OssClient::OSS_ACL_TYPE_PRIVATE;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putBucketAcl($bucket, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取存储空间的访问权限以下代码用于获取存储空间的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $res = $ossClient->getBucketAcl($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");print('acl: ' . $res); 获取存储空间的地域以下代码用于获取存储空间的地域(称为Region或Location): <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Regions = $ossClient->getBucketLocation($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Regions); 关于地域的详细信息请参见基本概念中的地域。 获取存储空间元信息以下代码用于获取存储空间元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Metas = $ossClient->getBucketMeta($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Metas); 删除存储空间删除存储空间之前,必须先删除存储空间下的所有文件、LiveChannel和分片上传产生的碎片。 以下代码用于删除存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteBucket($bucket);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");

2019-12-01 23:14:27 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档存储空间(Bucket)是对象(Object)的容器,对象必须隶属于某个存储空间。 以下场景的完整代码请参见GitHub。 创建存储空间以下代码用于创建存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 设置存储空间的存储类型为低频访问类型,默认是标准类型。 $options = array( OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA ); // 设置存储空间的权限为公共读,默认是私有读写。 $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储空间的命名规范,请参见基本概念中的命名规范。 判断存储空间是否存在以下代码用于判断指定的存储空间是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $res = $ossClient->doesBucketExist($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}if ($res === true) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");} 列举存储空间以下代码用于列举存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucketListInfo = $ossClient->listBuckets();} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}$bucketList = $bucketListInfo->getBucketList();foreach($bucketList as $bucket) { print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");} 设置存储空间的访问权限存储空间的访问权限(ACL)有以下三类: 访问权限 描述 访问权限值 私有 存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户没有权限操作该存储空间内的文件。 OssClient::OSS_ACL_TYPE_PRIVATE 公共读 存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户只有该存储空间内的文件的读权限。请谨慎使用该权限。 OssClient::OSS_ACL_TYPE_PUBLIC_READ 公共读写 所有用户都有该存储空间内的文件的读写权限。请谨慎使用该权限。 OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE 以下代码用于设置存储空间的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";// 设置存储空间的权限为私有。$acl = OssClient::OSS_ACL_TYPE_PRIVATE;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putBucketAcl($bucket, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取存储空间的访问权限以下代码用于获取存储空间的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $res = $ossClient->getBucketAcl($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");print('acl: ' . $res); 获取存储空间的地域以下代码用于获取存储空间的地域(称为Region或Location): <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Regions = $ossClient->getBucketLocation($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Regions); 关于地域的详细信息请参见基本概念中的地域。 获取存储空间元信息以下代码用于获取存储空间元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Metas = $ossClient->getBucketMeta($bucket);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Metas); 删除存储空间删除存储空间之前,必须先删除存储空间下的所有文件、LiveChannel和分片上传产生的碎片。 以下代码用于删除存储空间: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称。$bucket= "<yourBucketName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteBucket($bucket);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");

2019-12-01 23:14:27 0 浏览量 回答数 0

阿里云高校特惠,助力学生创业梦!0元体验,快速入门云计算!

学生动手场景应用,快速了解并掌握云服务器的各种新奇玩法!

回答

详细解答可以参考官方帮助文档您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub。 判断文件是否存在以下代码用于判断指定的文件是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $exist = $ossClient->doesObjectExist($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($exist); 文件访问权限文件的访问权限(ACL)有以下四种: 访问权限 描述 访问权限值 继承Bucket 文件遵循存储空间的访问权限。 default 私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private 公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read 公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write 文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。 设置文件访问权限以下代码用于设置指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 设置文件的访问权限为公共读,默认为继承Bucket。$acl = "public-read";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObjectAcl($bucket, $object, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件访问权限以下代码用于获取指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectAcl = $ossClient->getObjectAcl($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($objectAcl); 文件元信息文件元信息(Object Meta)详情请参见开发指南中的文件元信息。 设置文件元信息以下代码用于设置文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$content = file_get_contents(__FILE__);$options = array( OssClient::OSS_HEADERS => array( 'Expires' => '2012-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-self-define-title' => 'user define meta info', ));try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObject($bucket, $object, $content, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 修改文件元信息以下代码用于修改文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;/// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$fromBucket = "<yourFromBucketName>";$fromObject = "<yourFromObjectName>";$toBucket = "<yourToBucketName>";$toObject = "<yourToObjectName>";$copyOptions = array( OssClient::OSS_HEADERS => array( 'Expires' => '2018-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-location' => 'location', ),);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件元信息以下代码用于获取文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectMeta = $ossClient->getObjectMeta($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");if (isset($objectMeta[strtolower('Content-Disposition')]) && 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]) { print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");} else { print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");} 列举文件列举所有文件以下代码用于列举指定存储空间下的所有文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);while (true) { try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); $listPrefix = $listObjectInfo->getPrefixList(); if (!empty($listObject)) { print("objectList:\n"); foreach ($listObject as $objectInfo) { print($objectInfo->getKey() . "\n"); } } if (!empty($listPrefix)) { print("prefixList: \n"); foreach ($listPrefix as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } if ($nextMarker === '') { break; }} 列举指定条件的文件以下代码用于列举指定条件的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$prefix = 'dir/';$delimiter = '/';$nextMarker = '';$maxkeys = 10;$options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker,);try { $listObjectInfo = $ossClient->listObjects($bucket, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");$objectList = $listObjectInfo->getObjectList(); // object list$prefixList = $listObjectInfo->getPrefixList(); // directory listif (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); }}if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); }} 上述例子中的$options包含的参数如下: 参数 说明 是否必需 delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 否 prefix 本次查询结果的前缀。 否 max-keys 列举文件的最大个数。默认为100,最大值为1000。 否 marker 标明本次列举文件的起点。 否 删除文件删除单个文件以下代码用于删除单个文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObject($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 删除多个文件以下代码用于批量删除文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$objects = array();$objects[] = "<yourObjectName1>";$objects[] = "<yourObjectName2>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObjects($bucket, $objects);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 拷贝文件简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 解冻归档文件归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。 归档文件的状态变换过程如下: 归档类型的文件初始时处于冷冻状态。提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。 以下代码用于解冻归档文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->restoreObject($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储类型详情请参见存储类型。 符号链接创建符号链接符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。 以下代码用于创建符号链接: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putSymlink($bucket, $symlink, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取符号链接指向的文件内容获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Symlinks = $ossClient->getSymlink($bucket, $symlink);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Symlinks); 开启MD5校验MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。 以下代码用于上传文件时开启MD5校验: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$options = array(OssClient::OSS_CHECK_MD5 => true);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, __FILE__, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

2019-12-01 23:14:28 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub。 判断文件是否存在以下代码用于判断指定的文件是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $exist = $ossClient->doesObjectExist($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($exist); 文件访问权限文件的访问权限(ACL)有以下四种: 访问权限 描述 访问权限值 继承Bucket 文件遵循存储空间的访问权限。 default 私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private 公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read 公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write 文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。 设置文件访问权限以下代码用于设置指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 设置文件的访问权限为公共读,默认为继承Bucket。$acl = "public-read";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObjectAcl($bucket, $object, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件访问权限以下代码用于获取指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectAcl = $ossClient->getObjectAcl($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($objectAcl); 文件元信息文件元信息(Object Meta)详情请参见开发指南中的文件元信息。 设置文件元信息以下代码用于设置文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$content = file_get_contents(__FILE__);$options = array( OssClient::OSS_HEADERS => array( 'Expires' => '2012-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-self-define-title' => 'user define meta info', ));try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObject($bucket, $object, $content, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 修改文件元信息以下代码用于修改文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;/// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$fromBucket = "<yourFromBucketName>";$fromObject = "<yourFromObjectName>";$toBucket = "<yourToBucketName>";$toObject = "<yourToObjectName>";$copyOptions = array( OssClient::OSS_HEADERS => array( 'Expires' => '2018-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-location' => 'location', ),);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件元信息以下代码用于获取文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectMeta = $ossClient->getObjectMeta($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");if (isset($objectMeta[strtolower('Content-Disposition')]) && 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]) { print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");} else { print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");} 列举文件列举所有文件以下代码用于列举指定存储空间下的所有文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);while (true) { try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); $listPrefix = $listObjectInfo->getPrefixList(); if (!empty($listObject)) { print("objectList:\n"); foreach ($listObject as $objectInfo) { print($objectInfo->getKey() . "\n"); } } if (!empty($listPrefix)) { print("prefixList: \n"); foreach ($listPrefix as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } if ($nextMarker === '') { break; }} 列举指定条件的文件以下代码用于列举指定条件的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$prefix = 'dir/';$delimiter = '/';$nextMarker = '';$maxkeys = 10;$options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker,);try { $listObjectInfo = $ossClient->listObjects($bucket, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");$objectList = $listObjectInfo->getObjectList(); // object list$prefixList = $listObjectInfo->getPrefixList(); // directory listif (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); }}if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); }} 上述例子中的$options包含的参数如下: 参数 说明 是否必需 delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 否 prefix 本次查询结果的前缀。 否 max-keys 列举文件的最大个数。默认为100,最大值为1000。 否 marker 标明本次列举文件的起点。 否 删除文件删除单个文件以下代码用于删除单个文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObject($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 删除多个文件以下代码用于批量删除文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$objects = array();$objects[] = "<yourObjectName1>";$objects[] = "<yourObjectName2>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObjects($bucket, $objects);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 拷贝文件简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 解冻归档文件归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。 归档文件的状态变换过程如下: 归档类型的文件初始时处于冷冻状态。提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。 以下代码用于解冻归档文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->restoreObject($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储类型详情请参见存储类型。 符号链接创建符号链接符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。 以下代码用于创建符号链接: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putSymlink($bucket, $symlink, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取符号链接指向的文件内容获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Symlinks = $ossClient->getSymlink($bucket, $symlink);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Symlinks); 开启MD5校验MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。 以下代码用于上传文件时开启MD5校验: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$options = array(OssClient::OSS_CHECK_MD5 => true);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, __FILE__, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

2019-12-01 23:14:29 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub。 判断文件是否存在以下代码用于判断指定的文件是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $exist = $ossClient->doesObjectExist($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($exist); 文件访问权限文件的访问权限(ACL)有以下四种: 访问权限 描述 访问权限值 继承Bucket 文件遵循存储空间的访问权限。 default 私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private 公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read 公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write 文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。 设置文件访问权限以下代码用于设置指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 设置文件的访问权限为公共读,默认为继承Bucket。$acl = "public-read";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObjectAcl($bucket, $object, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件访问权限以下代码用于获取指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectAcl = $ossClient->getObjectAcl($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($objectAcl); 文件元信息文件元信息(Object Meta)详情请参见开发指南中的文件元信息。 设置文件元信息以下代码用于设置文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$content = file_get_contents(__FILE__);$options = array( OssClient::OSS_HEADERS => array( 'Expires' => '2012-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-self-define-title' => 'user define meta info', ));try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObject($bucket, $object, $content, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 修改文件元信息以下代码用于修改文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;/// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$fromBucket = "<yourFromBucketName>";$fromObject = "<yourFromObjectName>";$toBucket = "<yourToBucketName>";$toObject = "<yourToObjectName>";$copyOptions = array( OssClient::OSS_HEADERS => array( 'Expires' => '2018-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-location' => 'location', ),);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件元信息以下代码用于获取文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectMeta = $ossClient->getObjectMeta($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");if (isset($objectMeta[strtolower('Content-Disposition')]) && 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]) { print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");} else { print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");} 列举文件列举所有文件以下代码用于列举指定存储空间下的所有文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);while (true) { try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); $listPrefix = $listObjectInfo->getPrefixList(); if (!empty($listObject)) { print("objectList:\n"); foreach ($listObject as $objectInfo) { print($objectInfo->getKey() . "\n"); } } if (!empty($listPrefix)) { print("prefixList: \n"); foreach ($listPrefix as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } if ($nextMarker === '') { break; }} 列举指定条件的文件以下代码用于列举指定条件的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$prefix = 'dir/';$delimiter = '/';$nextMarker = '';$maxkeys = 10;$options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker,);try { $listObjectInfo = $ossClient->listObjects($bucket, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");$objectList = $listObjectInfo->getObjectList(); // object list$prefixList = $listObjectInfo->getPrefixList(); // directory listif (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); }}if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); }} 上述例子中的$options包含的参数如下: 参数 说明 是否必需 delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 否 prefix 本次查询结果的前缀。 否 max-keys 列举文件的最大个数。默认为100,最大值为1000。 否 marker 标明本次列举文件的起点。 否 删除文件删除单个文件以下代码用于删除单个文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObject($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 删除多个文件以下代码用于批量删除文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$objects = array();$objects[] = "<yourObjectName1>";$objects[] = "<yourObjectName2>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObjects($bucket, $objects);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 拷贝文件简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 解冻归档文件归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。 归档文件的状态变换过程如下: 归档类型的文件初始时处于冷冻状态。提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。 以下代码用于解冻归档文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->restoreObject($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储类型详情请参见存储类型。 符号链接创建符号链接符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。 以下代码用于创建符号链接: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putSymlink($bucket, $symlink, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取符号链接指向的文件内容获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Symlinks = $ossClient->getSymlink($bucket, $symlink);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Symlinks); 开启MD5校验MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。 以下代码用于上传文件时开启MD5校验: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$options = array(OssClient::OSS_CHECK_MD5 => true);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, __FILE__, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

2019-12-01 23:14:29 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub。 判断文件是否存在以下代码用于判断指定的文件是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $exist = $ossClient->doesObjectExist($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($exist); 文件访问权限文件的访问权限(ACL)有以下四种: 访问权限 描述 访问权限值 继承Bucket 文件遵循存储空间的访问权限。 default 私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private 公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read 公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write 文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。 设置文件访问权限以下代码用于设置指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 设置文件的访问权限为公共读,默认为继承Bucket。$acl = "public-read";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObjectAcl($bucket, $object, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件访问权限以下代码用于获取指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectAcl = $ossClient->getObjectAcl($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($objectAcl); 文件元信息文件元信息(Object Meta)详情请参见开发指南中的文件元信息。 设置文件元信息以下代码用于设置文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$content = file_get_contents(__FILE__);$options = array( OssClient::OSS_HEADERS => array( 'Expires' => '2012-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-self-define-title' => 'user define meta info', ));try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObject($bucket, $object, $content, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 修改文件元信息以下代码用于修改文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;/// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$fromBucket = "<yourFromBucketName>";$fromObject = "<yourFromObjectName>";$toBucket = "<yourToBucketName>";$toObject = "<yourToObjectName>";$copyOptions = array( OssClient::OSS_HEADERS => array( 'Expires' => '2018-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-location' => 'location', ),);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件元信息以下代码用于获取文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectMeta = $ossClient->getObjectMeta($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");if (isset($objectMeta[strtolower('Content-Disposition')]) && 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]) { print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");} else { print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");} 列举文件列举所有文件以下代码用于列举指定存储空间下的所有文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);while (true) { try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); $listPrefix = $listObjectInfo->getPrefixList(); if (!empty($listObject)) { print("objectList:\n"); foreach ($listObject as $objectInfo) { print($objectInfo->getKey() . "\n"); } } if (!empty($listPrefix)) { print("prefixList: \n"); foreach ($listPrefix as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } if ($nextMarker === '') { break; }} 列举指定条件的文件以下代码用于列举指定条件的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$prefix = 'dir/';$delimiter = '/';$nextMarker = '';$maxkeys = 10;$options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker,);try { $listObjectInfo = $ossClient->listObjects($bucket, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");$objectList = $listObjectInfo->getObjectList(); // object list$prefixList = $listObjectInfo->getPrefixList(); // directory listif (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); }}if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); }} 上述例子中的$options包含的参数如下: 参数 说明 是否必需 delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 否 prefix 本次查询结果的前缀。 否 max-keys 列举文件的最大个数。默认为100,最大值为1000。 否 marker 标明本次列举文件的起点。 否 删除文件删除单个文件以下代码用于删除单个文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObject($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 删除多个文件以下代码用于批量删除文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$objects = array();$objects[] = "<yourObjectName1>";$objects[] = "<yourObjectName2>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObjects($bucket, $objects);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 拷贝文件简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 解冻归档文件归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。 归档文件的状态变换过程如下: 归档类型的文件初始时处于冷冻状态。提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。 以下代码用于解冻归档文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->restoreObject($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储类型详情请参见存储类型。 符号链接创建符号链接符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。 以下代码用于创建符号链接: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putSymlink($bucket, $symlink, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取符号链接指向的文件内容获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Symlinks = $ossClient->getSymlink($bucket, $symlink);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Symlinks); 开启MD5校验MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。 以下代码用于上传文件时开启MD5校验: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$options = array(OssClient::OSS_CHECK_MD5 => true);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, __FILE__, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

2019-12-01 23:14:29 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub。 判断文件是否存在以下代码用于判断指定的文件是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $exist = $ossClient->doesObjectExist($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($exist); 文件访问权限文件的访问权限(ACL)有以下四种: 访问权限 描述 访问权限值 继承Bucket 文件遵循存储空间的访问权限。 default 私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private 公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read 公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write 文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。 设置文件访问权限以下代码用于设置指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 设置文件的访问权限为公共读,默认为继承Bucket。$acl = "public-read";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObjectAcl($bucket, $object, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件访问权限以下代码用于获取指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectAcl = $ossClient->getObjectAcl($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($objectAcl); 文件元信息文件元信息(Object Meta)详情请参见开发指南中的文件元信息。 设置文件元信息以下代码用于设置文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$content = file_get_contents(__FILE__);$options = array( OssClient::OSS_HEADERS => array( 'Expires' => '2012-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-self-define-title' => 'user define meta info', ));try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObject($bucket, $object, $content, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 修改文件元信息以下代码用于修改文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;/// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$fromBucket = "<yourFromBucketName>";$fromObject = "<yourFromObjectName>";$toBucket = "<yourToBucketName>";$toObject = "<yourToObjectName>";$copyOptions = array( OssClient::OSS_HEADERS => array( 'Expires' => '2018-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-location' => 'location', ),);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件元信息以下代码用于获取文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectMeta = $ossClient->getObjectMeta($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");if (isset($objectMeta[strtolower('Content-Disposition')]) && 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]) { print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");} else { print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");} 列举文件列举所有文件以下代码用于列举指定存储空间下的所有文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);while (true) { try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); $listPrefix = $listObjectInfo->getPrefixList(); if (!empty($listObject)) { print("objectList:\n"); foreach ($listObject as $objectInfo) { print($objectInfo->getKey() . "\n"); } } if (!empty($listPrefix)) { print("prefixList: \n"); foreach ($listPrefix as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } if ($nextMarker === '') { break; }} 列举指定条件的文件以下代码用于列举指定条件的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$prefix = 'dir/';$delimiter = '/';$nextMarker = '';$maxkeys = 10;$options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker,);try { $listObjectInfo = $ossClient->listObjects($bucket, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");$objectList = $listObjectInfo->getObjectList(); // object list$prefixList = $listObjectInfo->getPrefixList(); // directory listif (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); }}if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); }} 上述例子中的$options包含的参数如下: 参数 说明 是否必需 delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 否 prefix 本次查询结果的前缀。 否 max-keys 列举文件的最大个数。默认为100,最大值为1000。 否 marker 标明本次列举文件的起点。 否 删除文件删除单个文件以下代码用于删除单个文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObject($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 删除多个文件以下代码用于批量删除文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$objects = array();$objects[] = "<yourObjectName1>";$objects[] = "<yourObjectName2>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObjects($bucket, $objects);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 拷贝文件简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 解冻归档文件归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。 归档文件的状态变换过程如下: 归档类型的文件初始时处于冷冻状态。提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。 以下代码用于解冻归档文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->restoreObject($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储类型详情请参见存储类型。 符号链接创建符号链接符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。 以下代码用于创建符号链接: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putSymlink($bucket, $symlink, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取符号链接指向的文件内容获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Symlinks = $ossClient->getSymlink($bucket, $symlink);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Symlinks); 开启MD5校验MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。 以下代码用于上传文件时开启MD5校验: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$options = array(OssClient::OSS_CHECK_MD5 => true);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, __FILE__, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

2019-12-01 23:14:28 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub。 判断文件是否存在以下代码用于判断指定的文件是否存在: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $exist = $ossClient->doesObjectExist($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($exist); 文件访问权限文件的访问权限(ACL)有以下四种: 访问权限 描述 访问权限值 继承Bucket 文件遵循存储空间的访问权限。 default 私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private 公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read 公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write 文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。 设置文件访问权限以下代码用于设置指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 设置文件的访问权限为公共读,默认为继承Bucket。$acl = "public-read";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObjectAcl($bucket, $object, $acl);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件访问权限以下代码用于获取指定文件的访问权限: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectAcl = $ossClient->getObjectAcl($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($objectAcl); 文件元信息文件元信息(Object Meta)详情请参见开发指南中的文件元信息。 设置文件元信息以下代码用于设置文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$content = file_get_contents(__FILE__);$options = array( OssClient::OSS_HEADERS => array( 'Expires' => '2012-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-self-define-title' => 'user define meta info', ));try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putObject($bucket, $object, $content, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 修改文件元信息以下代码用于修改文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;/// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$fromBucket = "<yourFromBucketName>";$fromObject = "<yourFromObjectName>";$toBucket = "<yourToBucketName>";$toObject = "<yourToObjectName>";$copyOptions = array( OssClient::OSS_HEADERS => array( 'Expires' => '2018-10-01 08:00:00', 'Content-Disposition' => 'attachment; filename="xxxxxx"', 'x-oss-meta-location' => 'location', ),);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取文件元信息以下代码用于获取文件元信息: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $objectMeta = $ossClient->getObjectMeta($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");if (isset($objectMeta[strtolower('Content-Disposition')]) && 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]) { print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");} else { print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");} 列举文件列举所有文件以下代码用于列举指定存储空间下的所有文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);while (true) { try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); $listPrefix = $listObjectInfo->getPrefixList(); if (!empty($listObject)) { print("objectList:\n"); foreach ($listObject as $objectInfo) { print($objectInfo->getKey() . "\n"); } } if (!empty($listPrefix)) { print("prefixList: \n"); foreach ($listPrefix as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } if ($nextMarker === '') { break; }} 列举指定条件的文件以下代码用于列举指定条件的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$prefix = 'dir/';$delimiter = '/';$nextMarker = '';$maxkeys = 10;$options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker,);try { $listObjectInfo = $ossClient->listObjects($bucket, $options);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");$objectList = $listObjectInfo->getObjectList(); // object list$prefixList = $listObjectInfo->getPrefixList(); // directory listif (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); }}if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); }} 上述例子中的$options包含的参数如下: 参数 说明 是否必需 delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 否 prefix 本次查询结果的前缀。 否 max-keys 列举文件的最大个数。默认为100,最大值为1000。 否 marker 标明本次列举文件的起点。 否 删除文件删除单个文件以下代码用于删除单个文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObject($bucket, $object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 删除多个文件以下代码用于批量删除文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$objects = array();$objects[] = "<yourObjectName1>";$objects[] = "<yourObjectName2>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->deleteObjects($bucket, $objects);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 拷贝文件简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 解冻归档文件归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。 归档文件的状态变换过程如下: 归档类型的文件初始时处于冷冻状态。提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。 以下代码用于解冻归档文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->restoreObject($bucket, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 存储类型详情请参见存储类型。 符号链接创建符号链接符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。 以下代码用于创建符号链接: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->putSymlink($bucket, $symlink, $object);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); 获取符号链接指向的文件内容获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$symlink = "<yourSymlink>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $Symlinks = $ossClient->getSymlink($bucket, $symlink);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");var_dump($Symlinks); 开启MD5校验MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。 以下代码用于上传文件时开启MD5校验: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$options = array(OssClient::OSS_CHECK_MD5 => true);try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, __FILE__, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n"); putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

2019-12-01 23:14:29 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档使用签名URL进行临时授权您可以将生成的签名URL提供给访客进行临时访问。生成签名URL时,您可以指定URL的过期时间,来限制访客的访问时长。授权访问的完整代码请参见GitHub。 生成下载的签名URL以下代码用于生成下载的签名URL: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;use OSS\Http\RequestCore;use OSS\Http\ResponseCore;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";// 设置URL的有效期为3600秒。$timeout = 3600;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); // 生成GetObject的签名URL。 $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");// 可以使用代码来访问签名的URL,也可以输入到浏览器中进行访问。$request = new RequestCore($signedUrl);// 生成的URL默认以GET方式访问。$request->set_method('GET');$request->add_header('Content-Type', '');$request->send_request();$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());if ($res->isOK()) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");}; 生成上传的签名URL以下代码用于生成上传的签名URL: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;use OSS\Http\RequestCore;use OSS\Http\ResponseCore;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";$timeout = 3600;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); // 生成PutObject的签名URL。 $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT");} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");$content = "Hello OSS.";$request = new RequestCore($signedUrl);// 生成的URL以PUT方式访问。$request->set_method('PUT');$request->add_header('Content-Type', '');$request->add_header('Content-Length', strlen($content));$request->set_body($content);$request->send_request();$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());if ($res->isOK()) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");}; 使用STS进行临时授权您可以通过STS(Security Token Service)进行临时授权访问。更多有关STS的内容请参见访问控制API参考(STS)中的简介。关于账号及授权的详细信息请参见最佳实践中的STS临时授权访问。 以下代码用于使用STS临时授权上传文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";$content = "Hi, OSS.";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); $ossClient->putObject($bucket, $object, $content);} catch (OssException $e) { print $e->getMessage();}

2019-12-01 23:14:30 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档使用签名URL进行临时授权您可以将生成的签名URL提供给访客进行临时访问。生成签名URL时,您可以指定URL的过期时间,来限制访客的访问时长。授权访问的完整代码请参见GitHub。 生成下载的签名URL以下代码用于生成下载的签名URL: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;use OSS\Http\RequestCore;use OSS\Http\ResponseCore;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";// 设置URL的有效期为3600秒。$timeout = 3600;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); // 生成GetObject的签名URL。 $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");// 可以使用代码来访问签名的URL,也可以输入到浏览器中进行访问。$request = new RequestCore($signedUrl);// 生成的URL默认以GET方式访问。$request->set_method('GET');$request->add_header('Content-Type', '');$request->send_request();$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());if ($res->isOK()) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");}; 生成上传的签名URL以下代码用于生成上传的签名URL: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;use OSS\Http\RequestCore;use OSS\Http\ResponseCore;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";$timeout = 3600;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); // 生成PutObject的签名URL。 $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT");} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");$content = "Hello OSS.";$request = new RequestCore($signedUrl);// 生成的URL以PUT方式访问。$request->set_method('PUT');$request->add_header('Content-Type', '');$request->add_header('Content-Length', strlen($content));$request->set_body($content);$request->send_request();$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());if ($res->isOK()) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");}; 使用STS进行临时授权您可以通过STS(Security Token Service)进行临时授权访问。更多有关STS的内容请参见访问控制API参考(STS)中的简介。关于账号及授权的详细信息请参见最佳实践中的STS临时授权访问。 以下代码用于使用STS临时授权上传文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";$content = "Hi, OSS.";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); $ossClient->putObject($bucket, $object, $content);} catch (OssException $e) { print $e->getMessage();}

2019-12-01 23:14:30 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档使用签名URL进行临时授权您可以将生成的签名URL提供给访客进行临时访问。生成签名URL时,您可以指定URL的过期时间,来限制访客的访问时长。授权访问的完整代码请参见GitHub。 生成下载的签名URL以下代码用于生成下载的签名URL: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;use OSS\Http\RequestCore;use OSS\Http\ResponseCore;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";// 设置URL的有效期为3600秒。$timeout = 3600;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); // 生成GetObject的签名URL。 $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");// 可以使用代码来访问签名的URL,也可以输入到浏览器中进行访问。$request = new RequestCore($signedUrl);// 生成的URL默认以GET方式访问。$request->set_method('GET');$request->add_header('Content-Type', '');$request->send_request();$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());if ($res->isOK()) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");}; 生成上传的签名URL以下代码用于生成上传的签名URL: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;use OSS\Http\RequestCore;use OSS\Http\ResponseCore;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";$timeout = 3600;try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); // 生成PutObject的签名URL。 $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT");} catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");$content = "Hello OSS.";$request = new RequestCore($signedUrl);// 生成的URL以PUT方式访问。$request->set_method('PUT');$request->add_header('Content-Type', '');$request->add_header('Content-Length', strlen($content));$request->set_body($content);$request->send_request();$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());if ($res->isOK()) { print(__FUNCTION__ . ": OK" . "\n");} else { print(__FUNCTION__ . ": FAILED" . "\n");}; 使用STS进行临时授权您可以通过STS(Security Token Service)进行临时授权访问。更多有关STS的内容请参见访问控制API参考(STS)中的简介。关于账号及授权的详细信息请参见最佳实践中的STS临时授权访问。 以下代码用于使用STS临时授权上传文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";$securityToken = "<yourSecurityToken>";$content = "Hi, OSS.";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); $ossClient->putObject($bucket, $object, $content);} catch (OssException $e) { print $e->getMessage();}

2019-12-01 23:14:30 0 浏览量 回答数 0

回答

你直接引入包就可以了的 <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; }if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; }use OSSOssClient;use OSSCoreOssUtil;use OSSCoreOssException; try{ //putobject(); }catch (Exception $ex){ die($ex->getMessage()); } function putobject(){ $accessKeyId = " "; ; $accessKeySecret = " "; $endpoint = "oss-cn-hangzhou.aliyuncs.com"; $bucket= "bucket名称"; $object = "a.txt"; $content = "Hi, OSS."; try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $response = $ossClient->putObject($bucket, $object, $content); } catch (OssException $e) { print $e->getMessage(); } }类似这样的引入应用就可以了的

xiongmaodq 2019-12-02 01:39:41 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档简单拷贝以下代码用于拷贝小于1GB的文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$from_bucket = "<yourFromBucketName>";$from_object = "<yourFromObjectName>";$to_bucket = $bucket;$to_object = $from_object . '.copy';try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");` 拷贝大文件对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步: 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。通过$ossClient->completeMultipartUpload提交分片拷贝任务。 以下代码用于分片拷贝: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$src_bucket = "<yourSourceBucketName>";$src_object = "<yourSourceObjectName>";$dst_bucket = "<yourDestinationBucketName>";$dst_object = "<yourDestinationObjectName>";try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 初始化分片。 $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object); $copyId = 1; // 逐个分片拷贝。 $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id); $upload_parts[] = array( 'PartNumber' => $copyId, 'ETag' => $eTag, ); // 完成分片拷贝。 $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print(__FUNCTION__ . ": OK" . "\n");

2019-12-01 23:14:28 0 浏览量 回答数 0

回答

这个加载方式在Zend中早已被内化,例如一个在Vendor/MyClass.php中的类,可以用Vendor_MyClass来调用。怎么区分文件夹,这是需要自己写程序实现的。如下代码:function _vendor_autoload($class) { $parts = explode('\\', $class); # Support for non-namespaced classes. $parts[] = str_replace('_', DIRECTORY_SEPARATOR, array_pop($parts)); $path = implode(DIRECTORY_SEPARATOR, $parts); $file = stream_resolve_include_path($path.'.php'); if($file !== false) { require $file; } } spl_autoload_register('_vendor_autoload');

落地花开啦 2019-12-02 02:53:31 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档如果仅需要文件中的部分数据,您可以使用范围下载,下载指定范围内的数据,以下代码用于下载文件[0, 4]的内容到本地内存: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";$bucket= "<yourBucketName>";$object = "<yourObjectName>";// 获取0~4字节(包括0和4),共5个字节的数据。如果指定的范围无效(比如开始或结束位置的指定值为负数,或指定值大于文件大小),则下载整个文件。$options = array(OssClient::OSS_RANGE => '0-4');try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $content = $ossClient->getObject($bucket, $object, $options);} catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return;}print ($content);print(__FUNCTION__ . ": OK" . "\n");

2019-12-01 23:14:28 0 浏览量 回答数 0

问题

php中有关spl_autoload_register() 与 __autoload()效率对比

落地花开啦 2019-12-01 19:56:09 873 浏览量 回答数 1

回答

如果是通过composer安装的Yii,可以通过如下方式引入<?php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); // 注册 Composer 自动加载器 require(__DIR__ . '/../vendor/autoload.php'); // 包含 Yii 类文件 require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); // 加载应用配置 $config = require(__DIR__ . '/../config/web.php'); // 创建、配置、运行一个应用 (new yii\web\Application($config))->run();

昱冬 2019-12-01 23:48:01 0 浏览量 回答数 0

回答

字面上的意思是 curl操作出错:服务器返回为空建议您贴一下代码。这是实例:https://help.aliyun.com/document_detail/88473.html?source=5176.11533457&userCode=re2rax3m&type=copy <?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\OssClient; use OSS\Core\OssException; // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 $accessKeyId = "<yourAccessKeyId>"; $accessKeySecret = "<yourAccessKeySecret>"; // Endpoint以杭州为例,其它Region请按实际情况填写。 $endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 存储空间名称 $bucket= "<yourBucketName>"; // 文件名称 $object = "<yourObjectName>"; // <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt $filePath = "<yourLocalFile>"; try{ $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->uploadFile($bucket, $object, $filePath); } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");

云上珠玑 2019-12-02 01:56:57 0 浏览量 回答数 0

回答

详细解答可以参考官方帮助文档以下代码用于下载文件: <?phpif (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php';}if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php';}use OSS\OssClient;use OSS\Core\OssException;// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。$accessKeyId = "<yourAccessKeyId>";$accessKeySecret = "<yourAccessKeySecret>";// Endpoint以杭州为例,其它Region请按实际情况填写。$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";// 存储空间名称$bucket= "<yourBucketName>";// 文件名称$object = "<yourObjectName>";try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $content = $ossClient->getObject($bucket, $object); print("object content: " . $content);} catch (OssException $e) { print $e->getMessage();} 下载文件详情请参见下载文件。

2019-12-01 23:14:26 0 浏览量 回答数 0

回答

用PyODPS可以写成这样,mysql那边使用sqlalchemyimport itertools from sqlalchemy import create_engine, MetaData, Table from odps.df import DataFrame DB_CONNECT_STR = 'mysql+mysqldb://root:@localhost/mydb?charset=utf8' engine = create_engine(DB_CONNECT_STR, echo=True) conn = engine.connect() metadata = MetaData(engine) table = Table('mysql_table', metadata, autoload=True) df = DataFrame(odps.get_table('my_demo_table', project='my_project')) selected = df.filter(df.pdate == '')['imei', 'time_in', 'ntotalvote', 'ntotalcurr'] records = [] for i, record in zip(itertools.count(1), selected.execute()): if i % 100: conn.execute(conn.insert(), [dict(r) for r in records]) records = [] records.append(record) if records: conn.execute(conn.insert(), [dict(r) for r in records])

继盛 2019-12-02 03:18:19 0 浏览量 回答数 0

问题

php自定义框架类文件载入的疑问,报错

一枚小鲜肉帅哥 2020-06-20 21:14:48 0 浏览量 回答数 1

回答

(PHP 5 >= 5.1.2)spl_autoload_register — 注册__autoload()函数说明bool spl_autoload_register ([ callback $autoload_function ] )将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。如果在你的程序中已经实现......

小旋风柴进 2019-12-02 02:00:45 0 浏览量 回答数 0

问题

云盾提示有后门

巨浪 2019-12-01 21:23:57 9024 浏览量 回答数 3

问题

psr-4与spl_autoload_register 加载请教!?报错

爱吃鱼的程序员 2020-06-08 20:25:47 0 浏览量 回答数 1

问题

php 的 namespace 和 autoload 的问题

idevcloud 2019-12-01 19:50:54 1239 浏览量 回答数 1

问题

织梦整合七牛云储存

yahoo8 2019-12-01 20:57:39 3837 浏览量 回答数 0

问题

php open_basedir restriction in effect. 的问题?报错

爱吃鱼的程序员 2020-06-20 16:47:07 0 浏览量 回答数 1

问题

yaf,smarty问题? 400 报错

优选2 2020-06-09 11:00:51 1 浏览量 回答数 1

问题

yaf,smarty问题? 400 报错

爱吃鱼的程序员 2020-05-30 17:00:24 4 浏览量 回答数 1
阿里云大学 云服务器ECS com域名 网站域名whois查询 开发者平台 小程序定制 小程序开发 国内短信套餐包 开发者技术与产品 云数据库 图像识别 开发者问答 阿里云建站 阿里云备案 云市场 万网 阿里云帮助文档 免费套餐 开发者工具 企业信息查询 小程序开发制作 视频内容分析 企业网站制作 视频集锦 代理记账服务 企业建站模板