Alibaba Cloud oss operation tools

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


import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class AliyunOssUtil {

    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com"; 
    // Alibaba Cloud main account AccessKey has access to all APIs, and the risk is very high. It is strongly recommended that you create and use a RAM account for API access or daily operation and maintenance. Please log in to https://ram.console.aliyun.com to create a RAM account. 
    private static String accessKeyId = "<fill in own keyId>"; 
    private static String accessKeySecret = "<fill in own keyScecery>"; 

    private static String bucket = "<fill in own bucket>"; 

    / * 
    * upload local file 
    * parameters :
    * Bucket: storage space name 
    * fileName: specify the upload file name can specify the upload directory: directory name / file name 
    * localFilePath: specify the local file path 
    * * / 
    public static void uploadFile (String fileName, String localFilePath) { 
 
        // create OSSClient Examples.
        OSS ossClient = new OSSClientBuilder (). Build (endpoint, accessKeyId,

        // Create a PutObjectRequest object. 
        PutObjectRequest putObjectRequest = new PutObjectRequest (bucket, fileName, new File (localFilePath)); 

        // Upload the file. 
        ossClient.putObject (putObjectRequest); 

        // Close OSSClient. 
        ossClient.shutdown (); 
    } 

    / * 
     * Upload byte array 
     * Parameters: 
     * bucket: storage space name 
     * headImg: specify the file type of MultipartFile 
     * fileName: specify the upload file name can specify the upload directory: directory name / file name 
     * * / 
    public static void uploadFileBytes (MultipartFile file, String fileName) { 

        // Convert to byte array
        byte [] bytes = new byte [0]; 
        try { 
            bytes = file.getBytes (); 
        } catch (IOException e) { 
            e.printStackTrace (); 
        } 

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder (). Build (endpoint, accessKeyId, accessKeySecret); 

        // Upload Byte array. 
        ossClient.putObject (bucket, fileName, new ByteArrayInputStream (bytes)); 

        // Close OSSClient. 
        ossClient.shutdown (); 
    } 

    / * 
    * Delete 
    Alibaba Cloud file 
    * Parameters: * bucker: storage space name 
    * fileName: file name directory name / file name 
    * * / 
    public static void delete (String fileNme) {

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder (). Build (endpoint, accessKeyId, accessKeySecret); 
        // Set video interception operation.
 
        // delete the file. To delete a folder, set ObjectName to the corresponding folder name. If the folder is not empty, you need to delete all objects under the folder to delete the folder.
        ossClient.deleteObject (bucket, fileNme); 

        // Close OSSClient. 
        ossClient.shutdown (); 
    } 

   / * 
   * Video capture and upload to 
    Alibaba Cloud 
    * Parameters: * bucker: storage space name 
    * fileName: remote file file name directory name / file name 
    * coverName: intercepted cover name 
   * * / 
    public static void videoCoverIntercept (String fileNme, String coverName) { 

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder (). Build (endpoint, accessKeyId, accessKeySecret); 
        String style = "video / snapshot, t_1000, f_jpg, w_0, h_0"; 
        // Specify an expiration time of 10 minutes. 
        Date expiration = new Date (System.currentTimeMillis () + 1000 * 60 * 10); 
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest (bucket, fileNme, HttpMethod.GET); 
        req.setExpiration (expiration); 
        req.setProcess (style); 
        URL signedUrl = ossClient .generatePresignedUrl (req); 

        // Output picture network path 
        System.out.println (signedUrl); 

        // File upload to Alibaba Cloud 
        // Upload network stream. 
        InputStream inputStream = null; 
        try { 
            inputStream = new URL (signedUrl.toString ()). OpenStream (); 
            ossClient.putObject (bucket, coverName, inputStream); 
        } catch (IOException e) { 
            e.printStackTrace ();
        }
 
        // Close OSSClient. 
        ossClient.shutdown ();
        //sClient.shutdown (); 
    } 

    public static void main (String [] args) { 
        String bucket = ""; // Storage space name 
        String fileName = "picture.jpg"; // Specify the upload file name can specify the upload directory 
        String localFilePath = "G: \\ wallpaper \\ 104340-1562985820e760.jpg"; // Specify the local file path 
// 
// // Call the test method 
// uploadFile (bucket, fileName, localFilePath); 
        delete ("File name" ); 
    } 
}
Published 16 original articles · won praise 1 · views 3823

Guess you like

Origin blog.csdn.net/GYJ_love/article/details/105429752