云存储与CDN1. 技术分析1.1 云存储概述云存储是云计算的核心服务云存储类型 对象存储: S3、GCS、Blob 文件存储: EFS、Filestore 块存储: EBS、Persistent Disk 存储特性: 高可用: 多副本冗余 可扩展: 弹性扩容 持久化: 数据持久保存1.2 CDN概述内容分发网络加速内容传输CDN工作原理 缓存内容: 边缘节点缓存 就近访问: 用户就近获取 负载均衡: 流量分配 CDN优势: 降低延迟 提高可用性 减轻源站压力1.3 云存储对比类型适用场景性能价格对象存储静态资源中低文件存储共享存储高中块存储数据库很高中2. 核心功能实现2.1 对象存储管理import boto3 class ObjectStorageManager: def __init__(self): self.client boto3.client(s3) def create_bucket(self, bucket_name, regionus-east-1): if region us-east-1: response self.client.create_bucket(Bucketbucket_name) else: response self.client.create_bucket( Bucketbucket_name, CreateBucketConfiguration{LocationConstraint: region} ) return response def upload_object(self, bucket_name, file_path, object_key): with open(file_path, rb) as f: self.client.put_object( Bucketbucket_name, Keyobject_key, Bodyf, ContentTypeself._get_content_type(file_path) ) def download_object(self, bucket_name, object_key, local_path): response self.client.get_object(Bucketbucket_name, Keyobject_key) with open(local_path, wb) as f: f.write(response[Body].read()) def list_objects(self, bucket_name, prefix): response self.client.list_objects_v2(Bucketbucket_name, Prefixprefix) objects [] if Contents in response: for obj in response[Contents]: objects.append({ key: obj[Key], size: obj[Size], last_modified: obj[LastModified].isoformat() }) return objects def delete_object(self, bucket_name, object_key): self.client.delete_object(Bucketbucket_name, Keyobject_key) def _get_content_type(self, file_path): import mimetypes return mimetypes.guess_type(file_path)[0] or application/octet-stream2.2 CDN管理class CDNManager: def __init__(self): self.client boto3.client(cloudfront) def create_distribution(self, origin_domain, default_cache_behaviorNone): if default_cache_behavior is None: default_cache_behavior { TargetOriginId: my-origin, ViewerProtocolPolicy: redirect-to-https, AllowedMethods: { Quantity: 2, Items: [GET, HEAD], CachedMethods: { Quantity: 2, Items: [GET, HEAD] } }, DefaultTTL: 86400, MaxTTL: 31536000, MinTTL: 0 } response self.client.create_distribution( DistributionConfig{ Origins: { Quantity: 1, Items: [{ Id: my-origin, DomainName: origin_domain, CustomOriginConfig: { HTTPPort: 80, HTTPSPort: 443, OriginProtocolPolicy: https-only } }] }, DefaultCacheBehavior: default_cache_behavior, Enabled: True, Comment: My CDN Distribution } ) return { id: response[Distribution][Id], domain: response[Distribution][DomainName], status: response[Distribution][Status] } def invalidate_cache(self, distribution_id, paths): response self.client.create_invalidation( DistributionIddistribution_id, InvalidationBatch{ Paths: { Quantity: len(paths), Items: paths }, CallerReference: str(self._get_timestamp()) } ) return response[Invalidation][Id] def _get_timestamp(self): from datetime import datetime return datetime.now().timestamp()2.3 存储生命周期管理class StorageLifecycleManager: def __init__(self): self.client boto3.client(s3) def create_lifecycle_policy(self, bucket_name, rules): self.client.put_bucket_lifecycle_configuration( Bucketbucket_name, LifecycleConfiguration{ Rules: rules } ) def create_transition_rule(self, prefix, days, storage_class): return { ID: ftransition-{prefix}, Filter: {Prefix: prefix}, Status: Enabled, Transitions: [{ Days: days, StorageClass: storage_class }] } def create_expiration_rule(self, prefix, days): return { ID: fexpiration-{prefix}, Filter: {Prefix: prefix}, Status: Enabled, Expiration: {Days: days} }2.4 文件存储管理class FileStorageManager: def __init__(self): self.client boto3.client(efs) def create_file_system(self, creation_token, performance_modegeneralPurpose): response self.client.create_file_system( CreationTokencreation_token, PerformanceModeperformance_mode ) return { file_system_id: response[FileSystemId], creation_token: response[CreationToken], performance_mode: response[PerformanceMode] } def create_mount_target(self, file_system_id, subnet_id, security_groups): response self.client.create_mount_target( FileSystemIdfile_system_id, SubnetIdsubnet_id, SecurityGroupssecurity_groups ) return response[MountTargetId] def describe_file_systems(self): response self.client.describe_file_systems() file_systems [] for fs in response[FileSystems]: file_systems.append({ id: fs[FileSystemId], creation_time: fs[CreationTime].isoformat(), size_in_bytes: fs[SizeInBytes][Value], performance_mode: fs[PerformanceMode] }) return file_systems3. 性能对比3.1 云存储服务对比服务类型可用性最大对象大小AWS S3对象存储99.99%5TBAzure Blob对象存储99.99%4.75TBGCP GCS对象存储99.99%5TB3.2 CDN服务对比服务边缘节点数缓存类型SSL支持CloudFront400Web/RTMP支持Azure CDN110Web支持Cloudflare200Web支持3.3 存储类型对比类型访问方式延迟吞吐量对象存储HTTP/S中高文件存储NFS/SMB低中块存储iSCSI很低很高4. 最佳实践4.1 存储架构设计def design_storage_architecture(): obj_storage ObjectStorageManager() cdn CDNManager() lifecycle StorageLifecycleManager() # 创建存储桶 obj_storage.create_bucket(my-static-assets) # 创建CDN分发 cdn.create_distribution(my-static-assets.s3.amazonaws.com) # 设置生命周期策略 rules [ lifecycle.create_transition_rule(logs/, 30, STANDARD_IA), lifecycle.create_expiration_rule(temp/, 7) ] lifecycle.create_lifecycle_policy(my-static-assets, rules) return Storage architecture configured4.2 CDN缓存策略def configure_cdn_cache(): cache_config { DefaultTTL: 86400, # 24小时 MaxTTL: 31536000, # 1年 MinTTL: 0, AllowedMethods: [GET, HEAD], CachedMethods: [GET, HEAD], Compress: True, QueryString: False, Cookie: none, ForwardedValues: { QueryString: False, Cookies: {Forward: none} } } return cache_config5. 总结云存储和CDN是现代Web应用的基础设施对象存储存储静态资源CDN加速内容分发生命周期管理优化存储成本文件存储共享文件访问对比数据如下S3可用性最高(99.99%)CloudFront边缘节点最多(400)块存储延迟最低推荐使用生命周期策略优化成本云存储和CDN可以显著提升应用性能和用户体验。