全球主机交流论坛

标题: 腾讯qcloud cos对象存储免费50G的折腾分享 [打印本页]

作者: yumin9822    时间: 2016-11-25 16:33
标题: 腾讯qcloud cos对象存储免费50G的折腾分享
本帖最后由 yumin9822 于 2016-11-25 16:56 编辑

腾讯云的cos对象存储,免费容量50G,下载免费月流量 10G,上传流量免费,官方说法如下

产品价格
对象存储COS是一款后付费产品,每月 3-5 日生成上个月的账单并进行结算。所有用户拥有50G的免费存储空间,10G的免费IDC流量,10G的免费CDN回源流量,10万次PUT请求数和100万次GET请求数。超出部分按量计费,阶梯累进,更多内容,请点击查看文档>>
https://www.qcloud.com/doc/product/430/5871


官方提供了python php java C++ node.js ISO android 等SDK。本人试用了一下python sdk,做了一个简单的文件上传文件。对官方的sample.py做了一些小的修改和修正,只保留文件上传功能。增加参数传递。运行之前,请安装qcloud_cos, 官方只支持python 2.7。
小文件测试成功,5.6G大文件正在测试上传中。

利用下面脚本上传文件到腾讯对象存储的bucket中步骤
1. 安装python sdk,没有pip的请自行安装。
  1. pip install qcloud_cos
复制代码

2. 首先修改脚本里面的appid secret_id secret_key bucket
3. 复制修改后的脚本内容为文件qcoud_cos_file_upload.py,赋予运行权限
  1. chmod +x qcoud_cos_file_upload.py
复制代码

4. 假如你要上传的本地文件是 /foo/bar/sample.txt 远程存放在根目录并改名为sample2.txt。默认是远程文件存在则不覆盖,并返回错误。
  1. /path/to/qcoud_cos_file_upload.py /foo/bar/sample.txt /sample2.txt
复制代码



filename = qcoud_cos_file_upload.py
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-

  3. #"pip install qcloud_cos" before running this script
  4. import sys
  5. import qcloud_cos
  6. from qcloud_cos import CosClient
  7. from qcloud_cos import UploadFileRequest
  8. from qcloud_cos import UploadSliceFileRequest
  9. from qcloud_cos import UpdateFileRequest
  10. from qcloud_cos import UpdateFolderRequest
  11. from qcloud_cos import DelFileRequest
  12. from qcloud_cos import MoveFileRequest
  13. from qcloud_cos import DelFolderRequest
  14. from qcloud_cos import CreateFolderRequest
  15. from qcloud_cos import StatFileRequest
  16. from qcloud_cos import StatFolderRequest
  17. from qcloud_cos import ListFolderRequest

  18. from sys import getfilesystemencoding
  19. fs_encoding = getfilesystemencoding()

  20. def cos_file_upload( localfile, remotefile ):
  21.     # 设置用户属性, 包括appid, secret_id和secret_key
  22.     # 这些属性可以在cos控制台获取(https://console.qcloud.com/cos)
  23.     appid = 10012323                  # 替换为用户的appid
  24.     secret_id = 'gergegDZwt3gn4OJ'         # 替换为用户的secret_$
  25.     secret_key = 'bKxDwegwegPZTJRfbGDi'         # 替换为用户的secret_key
  26.     bucket = 'pub' # 替换为用户的要上传的bucket名称
  27.     # Unicode checking for 5 arguments
  28.     if not isinstance(secret_id, unicode):
  29.         secret_id = secret_id.decode(fs_encoding)
  30.     if not isinstance(secret_key, unicode):
  31.         secret_key = secret_key.decode(fs_encoding)
  32.     if not isinstance(bucket, unicode):
  33.         bucket = bucket.decode(fs_encoding)
  34.     if not isinstance(localfile, unicode):
  35.         localfile = localfile.decode(fs_encoding)
  36.     if not isinstance(remotefile, unicode):
  37.         remotefile = remotefile.decode(fs_encoding)
  38.     cos_client = CosClient(appid, secret_id, secret_key)
  39.     ############################################################################
  40.     # 文件操作                                                                 #
  41.     ############################################################################
  42.     # 1. 上传文件(默认不覆盖)
  43.     #    将本地的local_file_1.txt上传到bucket的根分区下,并命名为sample_file.txt
  44.     #    默认不覆盖, 如果cos上文件存在,则会返回错误
  45.     request = UploadFileRequest(bucket, remotefile, localfile)
  46.     upload_file_ret = cos_client.upload_file(request)
  47.     print 'upload file ret:', repr(upload_file_ret)

  48. if __name__ == '__main__':
  49.     #print sys.argv[1]
  50.     #print sys.argv[2]
  51.     cos_file_upload( sys.argv[1], sys.argv[2] )
复制代码


另外官方有提供目录同步工具,亲测好用
https://www.qcloud.com/doc/product/430/5919
下载地址:https://mc.qcloudimg.com/static/archive/7f448f635c2bbe606759048bfc73464c/cos_sync.zip





作者: yumin9822    时间: 2016-11-25 16:36
用来做纯备份空间还是不错。
网上流行的诸多备份脚本,配合我的python脚本增加上传到qcloud_cos,多一个地方,多一份安心!
反正上传流量不要钱。

作者: zrj766    时间: 2016-11-25 16:54
不错的干货
作者: EasonYang    时间: 2016-11-25 17:07
问一下COS的支持https么?
作者: mapleaf    时间: 2016-11-25 17:34
EasonYang 发表于 2016-11-25 17:07
问一下COS的支持https么?

支持,但是不含在免费流量内,需另计费。
作者: EasonYang    时间: 2016-11-25 17:37
mapleaf 发表于 2016-11-25 17:34
支持,但是不含在免费流量内,需另计费。

好吧,谢谢!
作者: 柊星瞳    时间: 2016-11-25 20:15
流量有点少  只能当备份用了




欢迎光临 全球主机交流论坛 (https://loc.isir333.eu.org/) Powered by Discuz! X3.4