【服务器】上传服务器中数据至 FigShare(Python)

张开发
2026/4/17 8:41:44 15 分钟阅读

分享文章

【服务器】上传服务器中数据至 FigShare(Python)
目录步骤 1将数据准备在服务器上步骤 2获取 Figshare API Token步骤 3在服务器上使用 Python 脚本上传案例1上传全球 LST 数据案例2上传全球 Ta 数据参考步骤 1将数据准备在服务器上如果数据已经在服务器 直接进行下一步。步骤 2获取 Figshare API Token1、登录 Figshare 网页版。2、点击右上角头像 - Integrations。3、在 “Personal Tokens” 下点击 Create Personal Token。4、保存好这个长字符串Token不要给别人看。随机生成如下 ad234363651b369fd8e1f6c72b575bd3b45c8aa71ab9n41c6ee78970b5fn6802594db64566fd517f2cb33b4d90b8644655c9b4a79498c9783a6b3217de587545、获取 Item ID从 Figshare 网页 URL 或 API 中找到那个已经创建好的 Item 的 ID。看浏览器地址栏通常是 https://figshare.com/account/articles/12345678。这个 12345678 就是 Article ID。https://datahub.hku.hk/account/items/12345678/ Article ID12345678步骤 3在服务器上使用 Python 脚本上传在服务器上Linux 或 Windows 均可创建一个 Python 脚本例如 upload_to_figshare.py使用该脚本可以将服务器上的大文件直接传至 Figshare。案例1上传全球 LST 数据服务器运行命令sourceactivate myenv3.10nohuppython upload_2021Day.py2021Day_log.txt21nohuppython-uupload_2021Day.py2021Day_log.txt21nohuppython-uupload_2021Nit.py2021Nit_log.txt21nohuppython-uupload_2022Day.py2022Day_log.txt21nohuppython-uupload_2022Nit.py2022Nit_log.txt21nohuppython-uupload_2023Day.py2023Day_log.txt21nohuppython-uupload_2023Nit.py2023Nit_log.txt21nohuppython-uupload_2024Day.py2024Day_log.txt21nohuppython-uupload_2024Nit.py2024Nit_log.txt21说明-u 代表 “unbuffered”无缓存输出会立刻写入 log 文件可以随时查看日志文件来监控进度tail-fupload_log.txtupload_figshare.py 文件内容如下importosimporthashlibimportjsonimportrequestsimportglob# 配置区域 (请修改这里) # 1. 您的 Figshare Personal TokenTOKEN这里填入您的长字符串Token# 2. 目标 Item 的 ID (从浏览器地址栏获取例如 25234567)# 注意必须是数字不要带引号或者转换成 intARTICLE_ID12345678# 3. 服务器上的数据文件夹路径SOURCE_DIR/geogfs1/groups/scl/Data/Global_LST/2021Day/# BASE_URLhttps://api.figshare.com/v2CHUNK_SIZE10*1024*1024# 10MB 分片大小defraw_issue_request(method,url,dataNone,binaryFalse):headers{Authorization:token TOKEN}ifdataisnotNoneandnotbinary:datajson.dumps(data)try:responserequests.request(method,url,headersheaders,datadata)response.raise_for_status()try:returnjson.loads(response.content)exceptValueError:returnresponse.contentexceptrequests.exceptions.HTTPErroraserror:print(fHTTP Error:{error})print(fResponse Body:{response.content})raiseerrordefget_uploaded_files(article_id):获取该 Item 下已经存在的文件列表用于去重endpointf{BASE_URL}/account/articles/{article_id}/filesfilesraw_issue_request(GET,endpoint)return{f[name]:f[id]forfinfiles}definitiate_upload(article_id,file_path):endpointf{BASE_URL}/account/articles/{article_id}/filesfile_nameos.path.basename(file_path)sizeos.path.getsize(file_path)# 计算 MD5print(fCalculating MD5 for{file_name}(Size:{size/1024/1024:.2f}MB)...)md5hashlib.md5()withopen(file_path,rb)asf:forchunkiniter(lambda:f.read(4096),b):md5.update(chunk)data{name:file_name,md5:md5.hexdigest(),size:size}resultraw_issue_request(POST,endpoint,data)locationresult[location]file_inforaw_issue_request(GET,location)print(fUpload initiated for{file_name})returnfile_infodefupload_parts(file_info,file_path):urlfile_info[upload_url]resultraw_issue_request(GET,url)partsresult[parts]withopen(file_path,rb)asfin:forpartinparts:part_nopart[partNo]start_offsetpart[startOffset]end_offsetpart[endOffset]fin.seek(start_offset)len_chunkend_offset-start_offset1datafin.read(len_chunk)# print(f - Uploading part {part_no}...) # 如果觉得刷屏太快可以注释掉raw_issue_request(PUT,f{url}/{part_no},data,binaryTrue)defcomplete_upload(article_id,file_id):raw_issue_request(POST,f{BASE_URL}/account/articles/{article_id}/files/{file_id})defmain():# 1. 获取待上传文件列表# 匹配文件夹下所有的 .tif 文件search_patternos.path.join(SOURCE_DIR,*.tif)files_to_uploadsorted(glob.glob(search_pattern))ifnotfiles_to_upload:print(fNo .tif files found in{SOURCE_DIR})returnprint(fFound{len(files_to_upload)}files to upload.)# 2. 获取已上传文件列表防止重复上传existing_filesget_uploaded_files(ARTICLE_ID)print(fAlready on Figshare:{len(existing_files)}files.)# 3. 循环上传forfile_pathinfiles_to_upload:file_nameos.path.basename(file_path)iffile_nameinexisting_files:print(f[Skipping]{file_name}already exists.)continueprint(f\n[Processing]{file_name}...)try:# 初始化file_infoinitiate_upload(ARTICLE_ID,file_path)# 上传分片upload_parts(file_info,file_path)# 确认完成complete_upload(ARTICLE_ID,file_info[id])print(f[Success]{file_name}uploaded.)exceptExceptionase:print(f[Failed] Error uploading{file_name}:{e})if__name____main__:main()案例2上传全球 Ta 数据参考

更多文章