Cara menggunakan Java untuk menyambung ke Storan Objek Awan Alibaba: mencapai pengurusan fail berskala besar dan kawalan akses
Perkhidmatan Penyimpanan Objek Awan Alibaba (OSS) ialah perkhidmatan storan awan yang sangat boleh dipercayai, selamat, kos rendah dan berskala tinggi , menyediakan perkhidmatan storan awan yang besar, selamat, kos rendah dan sangat boleh dipercayai. Artikel ini akan memperkenalkan cara menggunakan Java untuk menyambung ke Alibaba Cloud OSS untuk mencapai pengurusan fail dan kawalan akses berskala besar.
1. Persediaan
<dependencies> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.11.1</version> </dependency> </dependencies>
mvn clean package
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; public class OSSConnection { public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com"; public static final String accessKeyId = "your-access-key-id"; public static final String accessKeySecret = "your-access-key-secret"; public static void main(String[] args) { OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 连接成功后可以进行后续的文件管理和访问控制操作 ossClient.shutdown(); // 关闭连接 } }
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.PutObjectRequest; public class FileUpload { public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com"; public static final String accessKeyId = "your-access-key-id"; public static final String accessKeySecret = "your-access-key-secret"; public static final String bucketName = "your-bucket-name"; public static final String objectName = "your-object-name"; public static final String localFilePath = "your-local-file-path"; public static void main(String[] args) { OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(localFilePath)); ossClient.putObject(putObjectRequest); ossClient.shutdown(); } }
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.GetObjectRequest; public class FileDownload { public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com"; public static final String accessKeyId = "your-access-key-id"; public static final String accessKeySecret = "your-access-key-secret"; public static final String bucketName = "your-bucket-name"; public static final String objectName = "your-object-name"; public static final String localFilePath = "your-local-file-path"; public static void main(String[] args) { OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 下载文件 GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName); ossClient.getObject(getObjectRequest, new File(localFilePath)); ossClient.shutdown(); } }
perkhidmatan
rreee boleh akses OFF Laksanakan kawalan akses pada fail dan tetapkan kebenaran baca dan tulis pada fail. Contoh kod adalah seperti berikut:
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; public class FileDelete { public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com"; public static final String accessKeyId = "your-access-key-id"; public static final String accessKeySecret = "your-access-key-secret"; public static final String bucketName = "your-bucket-name"; public static final String objectName = "your-object-name"; public static void main(String[] args) { OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 删除文件 ossClient.deleteObject(bucketName, objectName); ossClient.shutdown(); } }
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.AccessControlList; import com.aliyun.oss.model.CannedAccessControlList; public class FileAccessControl { public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com"; public static final String accessKeyId = "your-access-key-id"; public static final String accessKeySecret = "your-access-key-secret"; public static final String bucketName = "your-bucket-name"; public static final String objectName = "your-object-name"; public static void main(String[] args) { OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 设置文件公共读权限 ossClient.setObjectAcl(bucketName, objectName, CannedAccessControlList.PublicRead); ossClient.shutdown(); } }
Atas ialah kandungan terperinci Cara menggunakan Java untuk menyambung ke Storan Objek Awan Alibaba: mencapai pengurusan fail dan kawalan akses berskala besar. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!