码迷,mamicode.com
首页 > Web开发 > 详细

oss 上传

时间:2020-06-20 14:29:09      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:包括   autoload   str   headers   使用   class   字符串   hang   rac   

https://help.aliyun.com/document_detail/88476.html?spm=a2c4g.11186623.6.1086.207134443kjsQz

 

字符串上传

<?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访问或日常运维,请登录RAM控制台创建RAM账号。
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 设置存储空间名称。
$bucket= "<yourBucketName>";
// 设置文件名称。
$object = "<yourObjectName>";
// 配置文件内容。
$content = "Hello OSS";
try{
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

    $ossClient->putObject($bucket, $object, $content);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK" . "\n");

// 上传时可以设置相关的headers,例如设置访问权限为private和自定义元信息。
$options = array(
    OssClient::OSS_HEADERS => array(
        ‘x-oss-object-acl‘ => ‘private‘,
        ‘x-oss-meta-info‘ => ‘your 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");           



文件上传
<?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访问或日常运维,请登录RAM控制台创建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");


oss 上传

标签:包括   autoload   str   headers   使用   class   字符串   hang   rac   

原文地址:https://www.cnblogs.com/keta/p/13168137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!