七牛雲--java擷取域空間檔案清單

來源:互聯網
上載者:User

      首先,註冊七牛雲,擷取密鑰,個人中心-->密鑰管理

   

        當然,我們可以在Object Storage Service中jia建立儲存空間,在內容管理中上傳圖片和下載圖片。在七牛上的每一張圖片都有自己單一的連結,但是隨著業務的擴充,需要訪問到一個域下面的所有圖片,圖片的具體名稱並不確定,或者以後會有變動。所以需要實現擷取空間檔案清單。

看了七牛雲的java SDK文檔實現過程如下:

//構造一個帶指定Zone對象的配置類Configuration cfg = new Configuration(Zone.zone0());//...其他參數參考類注釋String accessKey = "your access key";String secretKey = "your secret key";String bucket = "your bucket name";Auth auth = Auth.create(accessKey, secretKey);BucketManager bucketManager = new BucketManager(auth, cfg);//檔案名稱首碼String prefix = "";//每次迭代的長度限制,最大1000,推薦值 1000int limit = 1000;//指定目錄分隔字元,列出所有公用首碼(類比列出目錄效果)。預設值為空白字串String delimiter = "";//列舉空間檔案清單BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);while (fileListIterator.hasNext()) {    //處理擷取的file list結果    FileInfo[] items = fileListIterator.next();    for (FileInfo item : items) {        System.out.println(item.key);        System.out.println(item.hash);        System.out.println(item.fsize);        System.out.println(item.mimeType);        System.out.println(item.putTime);        System.out.println(item.endUser);    }}

       解釋一下上面的代碼,accessKey 就是AccessKey,secretKey 是你的SecretKey,bucket 是是個儲存空間的外鏈預設網域名稱。

        

        prefix為尋找在此域中的檔案首碼名稱,相當於按照首碼去查詢合格所有圖片。

        我們擷取到檔案清單之後,網路傳輸的是檔案流的形式,所以本地要使用,需要在代碼中對流進行轉化。


將FileInfo轉化成BufferedImage

//列舉空間檔案清單BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(BUCKET, prefix, limit, delimiter);FileInfo[] fileInfos = fileListIterator.next();Random rand = new Random();int index = rand.nextInt(fileInfos.length);FileInfo fileInfo = fileInfos[index];System.out.println("output:>>>" + fileInfo.key);ByteArrayInputStream in = new ByteArrayInputStream(getImageFromNetByUrl(BUCKET_HOST_NAME + "/" + fileInfo.key));@NonNull BufferedImage oriImage = ImageIO.read(in);return oriImage;

注意 BUCKET_HOST_NAME需要是拼接好網路通訊協定的,七牛雲預設是http。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.