想使用阿里云 OSS 存储图片,本地同样的环境下上传 4M 的图片到 OSS,按照帮助文档里的代码实现上传:
PHP 2 秒上传完成感觉速度是正常的,说明不是本地网络慢导致的,不知道 Java 版做了什么设置导致这么慢,经过多次反复测试时间都没什么变化,大家有遇到过这个情况吗,帮忙看看什么是原因导致的?
Java 代码:
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.File;
import java.io.FileNotFoundException;
/**
 * 上传文档: https://help.aliyun.com/document_detail/32013.html?spm=a2c4g.11186623.3.4.3ac339a5K0c1T7
 * 简单上传: https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.11186623.2.7.218d59aa5mAief#concept-84781-zh
 */
public class AliOss {
    public static void main(String[] args) throws FileNotFoundException {
        long start = System.currentTimeMillis();
        System.out.println("Start: " + start);
        // [1] Endpoint 以杭州为例,其它 Region 请按实际情况填写
        // [2] 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "xxx";
        String accessKeySecret = "xxx";
        // [3] 创建 OSSClient 实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // [4] 创建 PutObjectRequest 对象
        String filename =  System.currentTimeMillis() + ".jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest("biaomac", filename, new File("/Users/Biao/Pictures/storm.jpg"));
        // [6] 上传文件
        ossClient.putObject(putObjectRequest);
        // [7] 关闭 OSSClient
        ossClient.shutdown();
        // [8] 输出访问文件的 URL
        // URL 为 Bucket 域名 biaomac.oss-cn-beijing.aliyuncs.com 加上文件名字,
        // 如 https://biaomac.oss-cn-beijing.aliyuncs.com/avatar.jpg
        System.out.println("https://biaomac.oss-cn-beijing.aliyuncs.com/" + filename);
        System.out.println("End: " + (System.currentTimeMillis() - start));
    }
}
PHP 代码:
<?php
require_once __DIR__ . '/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
$endpoint = "oss-cn-beijing.aliyuncs.com";
$accessKeyId = "xxx";
$accessKeySecret = "xxx";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $content = file_get_contents('/Users/Biao/Pictures/storm.jpg');
    $options = array();
    $ossClient->putObject('biaomac', 'img/1.jpg', $content, $options);
} catch (OssException $e) {
    print $e->getMessage();
}
?>
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.