zoukankan      html  css  js  c++  java
  • 阿里云对象存储

    上传模型:

    查看阿里云关于文件上传的帮助: https://help.aliyun.com/document_detail/32009.html?spm=a2c4g.11186623.6.768.549d59aaWuZMGJ

    1)添加依赖包

    在Maven项目中加入依赖项(推荐方式)

    在 Maven 工程中使用 OSS Java SDK,只需在 pom.xml 中加入相应依赖即可。以 3.8.0 版本为例,在 内加入如下内容:

    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.8.0</version>
    </dependency>
    

      

    2)上传文件流

    以下代码用于上传文件流:

    // Endpoint以杭州为例,其它Region请按实际情况填写。
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    // 创建OSSClient实例。
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // 上传文件流。
    InputStream inputStream = new FileInputStream("<yourlocalFile>");
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
    
    // 关闭OSSClient。
    ossClient.shutdown();
    

      endpoint的取值:

    创建用户完毕后,会得到一个“AccessKey ID”和“AccessKeySecret”,然后复制这两个值到代码的“AccessKey ID”和“AccessKeySecret”。

    另外还需要添加访问控制权限:

    @Test
        public void testUpload() throws FileNotFoundException {
            // Endpoint以杭州为例,其它Region请按实际情况填写。
            String endpoint = "oss-cn-shanghai.aliyuncs.com";
            // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
            String accessKeyId = "LTAI4G4W1RA4JXz2QhoDwHhi";
            String accessKeySecret = "R99lmDOJumF2x43ZBKT259Qpe70Oxw";
    
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
            // 上传文件流。
            InputStream inputStream = new FileInputStream("C:\Users\Administrator\Pictures\timg.jpg");
            ossClient.putObject("gulimall-images", "time.jpg", inputStream);
    
            // 关闭OSSClient。
            ossClient.shutdown();
            System.out.println("上传成功.");
        }
    

      

    更为简单的使用方式,是使用SpringCloud Alibaba

  • 相关阅读:
    update jdk安装与配置
    Ubuntu 14.04 FTP服务器--vsftpd的安装和配置
    ubuntu 修改开机分辨率
    win7下通过easyBCD引导安装Ubuntu14.04
    IOS swift2.0 Get HTTP 数据请求
    oracle数据库中文汉字排序
    ORACLE日期函数大全!
    Oralce常用命令
    在linux环境下安装redis
    PHP常用函数大全
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14382324.html
Copyright © 2011-2022 走看看