日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

Java管理文件元數(shù)據(jù)

對象存儲(chǔ)OSS存儲(chǔ)的文件(Object)信息包含Key、Data和Object Meta。Object Meta是對文件的屬性描述,包括HTTP標(biāo)準(zhǔn)屬性(HTTP Header)和用戶自定義元數(shù)據(jù)(User Meta)兩種。您可以通過設(shè)置HTTP標(biāo)準(zhǔn)屬性來自定義HTTP請求的策略,例如文件(Object)緩存策略、強(qiáng)制下載策略等。您還可以通過設(shè)置用戶自定義元數(shù)據(jù)來標(biāo)識Object的用途或?qū)傩缘取?/p>

注意事項(xiàng)

  • 本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產(chǎn)品訪問OSS,請使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對應(yīng)關(guān)系,請參見訪問域名和數(shù)據(jù)中心

  • 本文以從環(huán)境變量讀取訪問憑證為例。如何配置訪問憑證,請參見Java配置訪問憑證

  • 本文以O(shè)SS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OSSClient

  • 要設(shè)置文件元數(shù)據(jù),您必須具有oss:PutObject權(quán)限;要獲取文件元數(shù)據(jù),您必須具有oss:GetObject權(quán)限。具體操作,請參見為RAM用戶授權(quán)自定義的權(quán)限策略

設(shè)置文件元數(shù)據(jù)

以下為設(shè)置HTTP標(biāo)準(zhǔn)屬性和自定義元數(shù)據(jù)的詳細(xì)示例。

  • 設(shè)置HTTP標(biāo)準(zhǔn)屬性

    以下代碼用于設(shè)置HTTP標(biāo)準(zhǔn)屬性。

    import com.aliyun.oss.ClientException;
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.common.auth.*;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.OSSException;
    import com.aliyun.oss.common.utils.BinaryUtil;
    import com.aliyun.oss.common.utils.DateUtil;
    import com.aliyun.oss.model.ObjectMetadata;
    import java.io.ByteArrayInputStream;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // Endpoint以華東1(杭州)為例,其它Region請按實(shí)際情況填寫。
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫B(tài)ucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫不包含Bucket名稱在內(nèi)的Object完整路徑,例如testfolder/exampleobject.txt。
            String objectName = "testfolder/exampleobject.txt";
            String content = "Hello OSS";
    
            // 創(chuàng)建OSSClient實(shí)例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
    
            try {
                // 創(chuàng)建上傳文件的元數(shù)據(jù)。
                ObjectMetadata meta = new ObjectMetadata();
    
                String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(content.getBytes()));
                // 開啟文件內(nèi)容MD5校驗(yàn)。開啟后OSS會(huì)把您提供的MD5與文件的MD5比較,不一致則拋出異常。
                meta.setContentMD5(md5);
                // 指定上傳的內(nèi)容類型。內(nèi)容類型決定瀏覽器將以什么形式、什么編碼讀取文件。如果沒有指定則根據(jù)文件的擴(kuò)展名生成,如果沒有擴(kuò)展名則為默認(rèn)值application/octet-stream。
                meta.setContentType("text/plain");
                // 設(shè)置內(nèi)容被下載時(shí)的名稱。
                meta.setContentDisposition("attachment; filename=\"DownloadFilename\"");
                // 設(shè)置上傳文件的長度。如超過此長度,則上傳文件會(huì)被截?cái)啵蟼鞯奈募L度為設(shè)置的長度。如小于此長度,則為上傳文件的實(shí)際長度。
                meta.setContentLength(content.length());
                // 設(shè)置內(nèi)容被下載時(shí)網(wǎng)頁的緩存行為。
                meta.setCacheControl("Download Action");
                // 設(shè)置緩存過期時(shí)間,格式是格林威治時(shí)間(GMT)。
                meta.setExpirationTime(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
                // 設(shè)置內(nèi)容被下載時(shí)的編碼格式。
                meta.setContentEncoding("gzip");
                // 設(shè)置Header。
                meta.setHeader("yourHeader", "yourHeaderValue");
    
                // 上傳文件。
                ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()), meta);
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
    }               

    關(guān)于HTTP Header的更多信息,請參見RFC2616

  • 設(shè)置自定義元數(shù)據(jù)

    您可以自定義文件的元數(shù)據(jù)來對文件進(jìn)行描述。

    以下代碼用于設(shè)置文件的自定義元數(shù)據(jù)。

    package com.aliyun.oss.demo;
    
    import com.aliyun.oss.ClientException;
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.common.auth.*;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.OSSException;
    import com.aliyun.oss.model.ObjectMetadata;
    import java.io.ByteArrayInputStream;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // Endpoint以華東1(杭州)為例,其它Region請按實(shí)際情況填寫。
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫B(tài)ucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫不包含Bucket名稱在內(nèi)的Object完整路徑,例如testfolder/exampleobject.txt。
            String objectName = "testfolder/exampleobject.txt";
            String content = "Hello OSS";
    
            // 創(chuàng)建OSSClient實(shí)例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
    
            try {
                // 創(chuàng)建文件元數(shù)據(jù)。
                ObjectMetadata meta = new ObjectMetadata();
                // 設(shè)置自定義元數(shù)據(jù)property值為property-value。建議使用Base64編碼。
                meta.addUserMetadata("property", "property-value");
    
                // 上傳文件。
                ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()), meta);            
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
    }                    

    下載文件時(shí),文件元數(shù)據(jù)也會(huì)同時(shí)下載。 一個(gè)文件可以有多個(gè)元數(shù)據(jù),總大小不能超過8 KB。

修改文件元數(shù)據(jù)

以下代碼用于修改文件的元數(shù)據(jù)。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.DateUtil;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;

public class Demo {
    public static void main(String[] args) throws Exception {
        // Endpoint以華東1(杭州)為例,其它Region請按實(shí)際情況填寫。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填寫源Bucket名稱。
        String sourceBucketName = "yourSourceBucketName";
        // 填寫源Object的完整路徑。
        String sourceObjectName = "yourSourceObjectName";
        // 填寫與源Bucket處于同一地域的目標(biāo)Bucket名稱。
        String destinationBucketName = "yourDestinationBucketName";
        // 填寫目標(biāo)Object的完整路徑。
        String destinationObjectName = "yourDestinationObjectName";

        // 創(chuàng)建OSSClient實(shí)例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 設(shè)置源文件與目標(biāo)文件相同,調(diào)用ossClient.copyObject方法修改文件元數(shù)據(jù)。
            CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);

            ObjectMetadata meta = new ObjectMetadata();
            // 指定上傳的內(nèi)容類型。內(nèi)容類型決定瀏覽器將以什么形式、什么編碼讀取文件。如果沒有指定則根據(jù)文件的擴(kuò)展名生成,如果沒有擴(kuò)展名則為默認(rèn)值application/octet-stream。
            meta.setContentType("text/plain");
            // 設(shè)置內(nèi)容被下載時(shí)的名稱。
            meta.setContentDisposition("Download File Name");
            // 設(shè)置內(nèi)容被下載時(shí)網(wǎng)頁的緩存行為。
            meta.setCacheControl("Download Action");
            // 設(shè)置緩存過期時(shí)間,格式是格林威治時(shí)間(GMT)。
            meta.setExpirationTime(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
            // 設(shè)置內(nèi)容被下載時(shí)的編碼格式。
            meta.setContentEncoding("gzip");
            // 設(shè)置header。
            meta.setHeader("<yourHeader>", "<yourHeaderValue>");
            // 設(shè)置自定義元數(shù)據(jù)property值為property-value。
            meta.addUserMetadata("property", "property-value");
            request.setNewObjectMetadata(meta);

            // 修改元數(shù)據(jù)。
            ossClient.copyObject(request);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}            

獲取文件元數(shù)據(jù)

您可以通過以下兩種方法獲取文件元數(shù)據(jù)。

方法

描述

ossClient.getSimplifiedObjectMeta

獲取文件的ETag、Size(文件大小)、 LastModified(最后修改時(shí)間)。該方法對應(yīng)的接口為GetObjectMeta

ossClient.getObjectMetadata

獲取文件的全部元數(shù)據(jù)。該方法對應(yīng)的接口為HeadObject

以下代碼用于獲取文件元數(shù)據(jù)。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.SimplifiedObjectMeta;

public class Demo {
    public static void main(String[] args) throws Exception {
        // Endpoint以華東1(杭州)為例,其它Region請按實(shí)際情況填寫。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填寫B(tài)ucket名稱,例如examplebucket。
        String bucketName = "examplebucket";
        // 填寫不包含Bucket名稱在內(nèi)的Object完整路徑,例如testfolder/exampleobject.txt。
        String objectName = "testfolder/exampleobject.txt";

        // 創(chuàng)建OSSClient實(shí)例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 依次填寫B(tài)ucket名稱以及Object的完整路徑。
            // 獲取文件的部分元數(shù)據(jù)。
            SimplifiedObjectMeta objectMeta = ossClient.getSimplifiedObjectMeta(bucketName, objectName);
            System.out.println(objectMeta.getSize());
            System.out.println(objectMeta.getETag());
            System.out.println(objectMeta.getLastModified());
            // 開啟訪問跟蹤功能后,用于獲取包含最后一次訪問時(shí)間(X-Oss-Last-Access-Time)在內(nèi)的文件元數(shù)據(jù)。僅Java SDK 3.16.0及以上版本支持獲取X-Oss-Last-Access-Time。
            System.out.println(objectMeta.getHeaders().get("x-oss-last-access-time"));

            // 獲取文件的全部元數(shù)據(jù)。
            ObjectMetadata metadata = ossClient.getObjectMetadata(bucketName, objectName);
            System.out.println(metadata.getContentType());
            System.out.println(metadata.getLastModified());
            System.out.println(metadata.getExpirationTime());
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

相關(guān)文檔

  • 關(guān)于文件元數(shù)據(jù)的更多內(nèi)容,請參見管理文件元數(shù)據(jù)

  • 關(guān)于設(shè)置和獲取文件元數(shù)據(jù)的完整示例代碼,請參見GitHub示例

  • 關(guān)于在簡單上傳過程中設(shè)置文件元數(shù)據(jù)的API接口說明,請參見PutObject

  • 關(guān)于獲取文件元數(shù)據(jù)的API接口說明,請參見GetObjectMetaHeadObject