本文提供了详细的阿里云OSS教程,帮助新手快速入门。内容涵盖了OSS的基础介绍、功能优势、账号注册和OSS服务开通方法,以及存储空间的创建和管理。此外,还详细讲解了文件的上传、下载和权限设置等操作,并提供了Python和Java SDK的示例代码。本文还覆盖了性能优化和成本控制技巧。
1. 阿里云OSS简介阿里云OSS(Object Storage Service)是一种面向阿里云用户的高性能、高可靠、可扩展的云存储服务。它提供了简单易用的RESTful API接口,使得用户可以对存储在OSS中的数据进行上传、下载和管理。阿里云OSS适用于各种场景,如静态网站托管、视频点播、图片处理、日志存储等。
登录阿里云账号后,点击顶部的“控制台”按钮,进入阿里云控制台。
import oss2 # 阿里云账号Access Key ID accessKeyId = 'yourAccessKeyId' # 阿里云账号Access Key Secret accessKeySecret = 'yourAccessKeySecret' # OSS存储空间(Bucket)名称 bucket_name = 'yourBucketName' # OSS存储空间(Bucket)所在的Endpoint endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # 创建Bucket对象 bucket = oss2.Bucket(oss2.Auth(accessKeyId, accessKeySecret), endpoint, bucket_name) # 设置Bucket属性 def set_bucket_properties(bucket_name, storage_type): bucket.put_bucket_storage_class(bucket_name, storage_type) # 示例代码 set_bucket_properties(bucket_name, 'STANDARD')
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; public class Example { public static void main(String[] args) { // 阿里云账号Access Key ID String accessKeyId = "yourAccessKeyId"; // 阿里云账号Access Key Secret String accessKeySecret = "yourAccessKeySecret"; // OSS存储空间(Bucket)名称 String bucketName = "yourBucketName"; // OSS存储空间(Bucket)所在的Endpoint String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 创建OSSClient实例 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 设置Bucket属性 public void setBucketProperties(String bucketName, String storageType) { ossClient.setBucketStorageClass(bucketName, storageType); } // 示例代码 setBucketProperties(bucketName, "STANDARD"); // 关闭OSSClient ossClient.shutdown(); } }3. 上传和下载文件
# 安装阿里云OSS SDK !pip install oss2 import oss2 # 阿里云账号Access Key ID accessKeyId = 'yourAccessKeyId' # 阿里云账号Access Key Secret accessKeySecret = 'yourAccessKeySecret' # OSS存储空间(Bucket)名称 bucket_name = 'yourBucketName' # OSS存储空间(Bucket)所在的Endpoint endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # 创建Bucket对象 bucket = oss2.Bucket(oss2.Auth(accessKeyId, accessKeySecret), endpoint, bucket_name) # 上传文件 def upload_file(local_file_path, oss_key): bucket.put_object_from_file(oss_key, local_file_path) # 下载文件 def download_file(oss_key, local_file_path): bucket.get_bucket.get_object_to_file(oss_key, local_file_path) # 示例代码 upload_file('/path/to/local/file', 'oss_key') download_file('oss_key', '/path/to/local/file')
// 安装阿里云OSS SDK // 添加依赖项 <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.3</version> </dependency> import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; public class Example { public static void main(String[] args) { // 阿里云账号Access Key ID String accessKeyId = "yourAccessKeyId"; // 阿里云账号Access Key Secret String accessKeySecret = "yourAccessKeySecret"; // OSS存储空间(Bucket)名称 String bucketName = "yourBucketName"; // OSS存储空间(Bucket)所在的Endpoint String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 创建OSSClient实例 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件 String objectName = "example.txt"; String localFile = "/path/to/local/file"; ossClient.putObject(bucketName, objectName, new File(localFile)); // 下载文件 String output = "/path/to/local/file"; ossClient.getObject(bucketName, objectName, new File(output)); // 关闭OSSClient ossClient.shutdown(); } }4. 管理文件和目录
def delete_file(oss_key): bucket.delete_object(oss_key) # 示例代码 delete_file('oss_key')
public void deleteFile(String objectName) { ossClient.deleteObject(bucketName, objectName); } // 示例代码 deleteFile("oss_key");
def create_directory(directory_name): bucket.put_object(directory_name + "/") # 示例代码 create_directory('directory_name')
public void createDirectory(String directoryName) { ossClient.putObject(bucketName, directoryName + "/", new ByteArrayInputStream(new byte[0])); } // 示例代码 createDirectory("directory_name");
def move_file(old_key, new_key): copy_source = {'Bucket': bucket_name, 'Key': old_key} bucket.copy_object(copy_source, new_key) bucket.delete_object(old_key) # 示例代码 move_file('old_key', 'new_key')
public void moveFile(String oldObject, String newObject) { ossClient.copyObject(bucketName, oldObject, bucketName, newObject); ossClient.deleteObject(bucketName, oldObject); } // 示例代码 moveFile("old_key", "new_key");5. 设置权限和安全性
def set_bucket_permission(permission): bucket.put_bucket_acl(permission) # 示例代码 set_bucket_permission('public-read')
public void setBucketPermission(String acl) { ossClient.setBucketAcl(bucketName, acl); } // 示例代码 setBucketPermission("public-read");
def get_presigned_url(oss_key, expires): return bucket.presigned_url('GET', oss_key, expires=expires) # 示例代码 url = get_presigned_url('oss_key', 3600) print(url)
public String getPresignedUrl(String objectName, long expires) { return ossClient.generatePresignedUrl(bucketName, objectName, new Date(new Date().getTime() + expires * 1000)).toString(); } // 示例代码 String url = getPresignedUrl("oss_key", 3600); System.out.println(url);
import json def set_bucket_policy(policy): bucket.put_bucket_policy(policy) # 示例代码 policy = { "Statement": [ { "Action": ["oss:ListBucket"], "Effect": "Allow", "Principal": "*", "Resource": [ f"acs:oss:*:*:{bucket_name}" ] } ], "Version": "1" } set_bucket_policy(json.dumps(policy))
import com.aliyun.oss.model.Acl; import com.aliyun.oss.model.BucketPolicy; public void setBucketPolicy(String policy) { bucket.putBucketPolicy(policy); } // 示例代码 String policy = "{\"Statement\":[{\"Action\":[\"oss:ListBucket\"],\"Effect\":\"Allow\",\"Principal\":\"*\"," + "\"Resource\":[\"acs:oss:*:*:" + bucketName + "\"]}],\"Version\":\"1\"}"; setBucketPolicy(policy);6. 常见问题与解答
错误代码:NoSuchBucket