官方文檔沒有實例,因此記錄一下
<dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>7.2.29</version> </dependency>
/** * 復制七牛雲文件 * @param fromBucket 源空間,這里在七牛雲控制台獲取,例如:test-bucket * @param fromKey 源文件名稱 文件訪問路徑域名后面的值,例如:u/132/10000132/202110/o/3251c4594a8f4fb08ab85f35c66870e3.png * @param toBucket 目標空間,同上fromBucket * @param toKey 目標文件名稱,同上fromKey,這個可以自定義,例如:avatar/o/3667/13667/d267f07a63db491ba629c54bf87f034c.jpg */ public static void copyFile(String fromBucket,String fromKey,String toBucket,String toKey){ //構造一個帶指定 Region 對象的配置類 Configuration cfg = new Configuration(Region.region0()); //accessKey,secretKey控制台配置的 Auth auth = Auth.create(accessKey, secretKey); BucketManager bucketManager = new BucketManager(auth, cfg); try { bucketManager.copy(fromBucket, fromKey, toBucket, toKey); } catch (QiniuException ex) { //如果遇到異常,說明移動失敗 LogUtil.showData("===>>>執行復制七牛雲文件失敗:exception="+ex.response.toString()); } }