s3cmd 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ## --------------------------------------------------------------------
  4. ## s3cmd - S3 client
  5. ##
  6. ## Authors : Michal Ludvig and contributors
  7. ## Copyright : TGRMN Software - http://www.tgrmn.com - and contributors
  8. ## Website : http://s3tools.org
  9. ## License : GPL Version 2
  10. ## --------------------------------------------------------------------
  11. ## This program is free software; you can redistribute it and/or modify
  12. ## it under the terms of the GNU General Public License as published by
  13. ## the Free Software Foundation; either version 2 of the License, or
  14. ## (at your option) any later version.
  15. ## This program is distributed in the hope that it will be useful,
  16. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ## GNU General Public License for more details.
  19. ## --------------------------------------------------------------------
  20. from __future__ import absolute_import, print_function, division
  21. import sys
  22. if sys.version_info < (2, 6):
  23. sys.stderr.write(u"ERROR: Python 2.6 or higher required, sorry.\n")
  24. # 72 == EX_OSFILE
  25. sys.exit(72)
  26. PY3 = (sys.version_info >= (3, 0))
  27. import codecs
  28. import errno
  29. import glob
  30. import io
  31. import locale
  32. import logging
  33. import os
  34. import re
  35. import shutil
  36. import socket
  37. import subprocess
  38. import tempfile
  39. import time
  40. import traceback
  41. from copy import copy
  42. from optparse import OptionParser, Option, OptionValueError, IndentedHelpFormatter
  43. from logging import debug, info, warning, error
  44. try:
  45. import htmlentitydefs
  46. except Exception:
  47. # python 3 support
  48. import html.entities as htmlentitydefs
  49. try:
  50. unicode
  51. except NameError:
  52. # python 3 support
  53. # In python 3, unicode -> str, and str -> bytes
  54. unicode = str
  55. try:
  56. unichr
  57. except NameError:
  58. # python 3 support
  59. # In python 3, unichr was removed as chr can now do the job
  60. unichr = chr
  61. try:
  62. from shutil import which
  63. except ImportError:
  64. # python2 fallback code
  65. from distutils.spawn import find_executable as which
  66. if not PY3:
  67. # ConnectionRefusedError does not exist in python2
  68. class ConnectionError(OSError):
  69. pass
  70. class ConnectionRefusedError(ConnectionError):
  71. pass
  72. def output(message):
  73. sys.stdout.write(message + "\n")
  74. sys.stdout.flush()
  75. def check_args_type(args, type, verbose_type):
  76. """NOTE: This function looks like to not be used."""
  77. for arg in args:
  78. if S3Uri(arg).type != type:
  79. raise ParameterError("Expecting %s instead of '%s'" % (verbose_type, arg))
  80. def cmd_du(args):
  81. s3 = S3(Config())
  82. if len(args) > 0:
  83. uri = S3Uri(args[0])
  84. if uri.type == "s3" and uri.has_bucket():
  85. subcmd_bucket_usage(s3, uri)
  86. return EX_OK
  87. subcmd_bucket_usage_all(s3)
  88. return EX_OK
  89. def subcmd_bucket_usage_all(s3):
  90. """
  91. Returns: sum of bucket sizes as integer
  92. Raises: S3Error
  93. """
  94. cfg = Config()
  95. response = s3.list_all_buckets()
  96. buckets_size = 0
  97. for bucket in response["list"]:
  98. size = subcmd_bucket_usage(s3, S3Uri("s3://" + bucket["Name"]))
  99. if size != None:
  100. buckets_size += size
  101. total_size, size_coeff = formatSize(buckets_size, cfg.human_readable_sizes)
  102. total_size_str = str(total_size) + size_coeff
  103. output(u"".rjust(12, "-"))
  104. output(u"%s Total" % (total_size_str.ljust(12)))
  105. return size
  106. def subcmd_bucket_usage(s3, uri):
  107. """
  108. Returns: bucket size as integer
  109. Raises: S3Error
  110. """
  111. bucket_size = 0
  112. object_count = 0
  113. extra_info = u''
  114. bucket = uri.bucket()
  115. prefix = uri.object()
  116. try:
  117. for _, _, objects in s3.bucket_list_streaming(bucket, prefix=prefix, recursive=True):
  118. for obj in objects:
  119. bucket_size += int(obj["Size"])
  120. object_count += 1
  121. except S3Error as e:
  122. if e.info["Code"] in S3.codes:
  123. error(S3.codes[e.info["Code"]] % bucket)
  124. raise
  125. except KeyboardInterrupt as e:
  126. extra_info = u' [interrupted]'
  127. total_size_str = u"%d%s" % formatSize(bucket_size,
  128. Config().human_readable_sizes)
  129. if Config().human_readable_sizes:
  130. total_size_str = total_size_str.rjust(5)
  131. else:
  132. total_size_str = total_size_str.rjust(12)
  133. output(u"%s %7s objects %s%s" % (total_size_str, object_count, uri,
  134. extra_info))
  135. return bucket_size
  136. def cmd_ls(args):
  137. cfg = Config()
  138. s3 = S3(cfg)
  139. if len(args) > 0:
  140. uri = S3Uri(args[0])
  141. if uri.type == "s3" and uri.has_bucket():
  142. subcmd_bucket_list(s3, uri, cfg.limit)
  143. return EX_OK
  144. # If not a s3 type uri or no bucket was provided, list all the buckets
  145. subcmd_all_buckets_list(s3)
  146. return EX_OK
  147. def subcmd_all_buckets_list(s3):
  148. response = s3.list_all_buckets()
  149. for bucket in sorted(response["list"], key=lambda b:b["Name"]):
  150. output(u"%s s3://%s" % (formatDateTime(bucket["CreationDate"]),
  151. bucket["Name"]))
  152. def cmd_all_buckets_list_all_content(args):
  153. cfg = Config()
  154. s3 = S3(cfg)
  155. response = s3.list_all_buckets()
  156. for bucket in response["list"]:
  157. subcmd_bucket_list(s3, S3Uri("s3://" + bucket["Name"]), cfg.limit)
  158. output(u"")
  159. return EX_OK
  160. def subcmd_bucket_list(s3, uri, limit):
  161. cfg = Config()
  162. bucket = uri.bucket()
  163. prefix = uri.object()
  164. debug(u"Bucket 's3://%s':" % bucket)
  165. if prefix.endswith('*'):
  166. prefix = prefix[:-1]
  167. try:
  168. response = s3.bucket_list(bucket, prefix = prefix, limit = limit)
  169. except S3Error as e:
  170. if e.info["Code"] in S3.codes:
  171. error(S3.codes[e.info["Code"]] % bucket)
  172. raise
  173. # md5 are 32 char long, but for multipart there could be a suffix
  174. if Config().human_readable_sizes:
  175. # %(size)5s%(coeff)1s
  176. format_size = u"%5d%1s"
  177. dir_str = u"DIR".rjust(6)
  178. else:
  179. format_size = u"%12d%s"
  180. dir_str = u"DIR".rjust(12)
  181. if cfg.long_listing:
  182. format_string = u"%(timestamp)16s %(size)s %(md5)-35s %(storageclass)-11s %(uri)s"
  183. elif cfg.list_md5:
  184. format_string = u"%(timestamp)16s %(size)s %(md5)-35s %(uri)s"
  185. else:
  186. format_string = u"%(timestamp)16s %(size)s %(uri)s"
  187. for prefix in response['common_prefixes']:
  188. output(format_string % {
  189. "timestamp": "",
  190. "size": dir_str,
  191. "md5": "",
  192. "storageclass": "",
  193. "uri": uri.compose_uri(bucket, prefix["Prefix"])})
  194. for object in response["list"]:
  195. md5 = object.get('ETag', '').strip('"\'')
  196. storageclass = object.get('StorageClass','')
  197. if cfg.list_md5:
  198. if '-' in md5: # need to get md5 from the object
  199. object_uri = uri.compose_uri(bucket, object["Key"])
  200. info_response = s3.object_info(S3Uri(object_uri))
  201. try:
  202. md5 = info_response['s3cmd-attrs']['md5']
  203. except KeyError:
  204. pass
  205. size_and_coeff = formatSize(object["Size"],
  206. Config().human_readable_sizes)
  207. output(format_string % {
  208. "timestamp": formatDateTime(object["LastModified"]),
  209. "size" : format_size % size_and_coeff,
  210. "md5" : md5,
  211. "storageclass" : storageclass,
  212. "uri": uri.compose_uri(bucket, object["Key"]),
  213. })
  214. if response["truncated"]:
  215. warning(u"The list is truncated because the settings limit was reached.")
  216. def cmd_bucket_create(args):
  217. cfg = Config()
  218. s3 = S3(cfg)
  219. for arg in args:
  220. uri = S3Uri(arg)
  221. if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
  222. raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
  223. try:
  224. response = s3.bucket_create(uri.bucket(), cfg.bucket_location, cfg.extra_headers)
  225. output(u"Bucket '%s' created" % uri.uri())
  226. except S3Error as e:
  227. if e.info["Code"] in S3.codes:
  228. error(S3.codes[e.info["Code"]] % uri.bucket())
  229. raise
  230. return EX_OK
  231. def cmd_website_info(args):
  232. cfg = Config()
  233. s3 = S3(cfg)
  234. for arg in args:
  235. uri = S3Uri(arg)
  236. if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
  237. raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
  238. try:
  239. response = s3.website_info(uri, cfg.bucket_location)
  240. if response:
  241. output(u"Bucket %s: Website configuration" % uri.uri())
  242. output(u"Website endpoint: %s" % response['website_endpoint'])
  243. output(u"Index document: %s" % response['index_document'])
  244. output(u"Error document: %s" % response['error_document'])
  245. else:
  246. output(u"Bucket %s: No website configuration found." % (uri.uri()))
  247. except S3Error as e:
  248. if e.info["Code"] in S3.codes:
  249. error(S3.codes[e.info["Code"]] % uri.bucket())
  250. raise
  251. return EX_OK
  252. def cmd_website_create(args):
  253. cfg = Config()
  254. s3 = S3(cfg)
  255. for arg in args:
  256. uri = S3Uri(arg)
  257. if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
  258. raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
  259. try:
  260. response = s3.website_create(uri, cfg.bucket_location)
  261. output(u"Bucket '%s': website configuration created." % (uri.uri()))
  262. except S3Error as e:
  263. if e.info["Code"] in S3.codes:
  264. error(S3.codes[e.info["Code"]] % uri.bucket())
  265. raise
  266. return EX_OK
  267. def cmd_website_delete(args):
  268. cfg = Config()
  269. s3 = S3(cfg)
  270. for arg in args:
  271. uri = S3Uri(arg)
  272. if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
  273. raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
  274. try:
  275. response = s3.website_delete(uri, cfg.bucket_location)
  276. output(u"Bucket '%s': website configuration deleted." % (uri.uri()))
  277. except S3Error as e:
  278. if e.info["Code"] in S3.codes:
  279. error(S3.codes[e.info["Code"]] % uri.bucket())
  280. raise
  281. return EX_OK
  282. def cmd_expiration_set(args):
  283. cfg = Config()
  284. s3 = S3(cfg)
  285. for arg in args:
  286. uri = S3Uri(arg)
  287. if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
  288. raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
  289. try:
  290. response = s3.expiration_set(uri, cfg.bucket_location)
  291. if response["status"] == 200:
  292. output(u"Bucket '%s': expiration configuration is set." % (uri.uri()))
  293. elif response["status"] == 204:
  294. output(u"Bucket '%s': expiration configuration is deleted." % (uri.uri()))
  295. except S3Error as e:
  296. if e.info["Code"] in S3.codes:
  297. error(S3.codes[e.info["Code"]] % uri.bucket())
  298. raise
  299. return EX_OK
  300. def cmd_bucket_delete(args):
  301. cfg = Config()
  302. s3 = S3(cfg)
  303. def _bucket_delete_one(uri, retry=True):
  304. try:
  305. response = s3.bucket_delete(uri.bucket())
  306. output(u"Bucket '%s' removed" % uri.uri())
  307. except S3Error as e:
  308. if e.info['Code'] == 'NoSuchBucket':
  309. if cfg.force:
  310. return EX_OK
  311. else:
  312. raise
  313. if e.info['Code'] == 'BucketNotEmpty' and retry and (cfg.force or cfg.recursive):
  314. warning(u"Bucket is not empty. Removing all the objects from it first. This may take some time...")
  315. rc = subcmd_batch_del(uri_str = uri.uri())
  316. if rc == EX_OK:
  317. return _bucket_delete_one(uri, False)
  318. else:
  319. output(u"Bucket was not removed")
  320. elif e.info["Code"] in S3.codes:
  321. error(S3.codes[e.info["Code"]] % uri.bucket())
  322. raise
  323. return EX_OK
  324. for arg in args:
  325. uri = S3Uri(arg)
  326. if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
  327. raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
  328. rc = _bucket_delete_one(uri)
  329. if rc != EX_OK:
  330. return rc
  331. return EX_OK
  332. def cmd_object_put(args):
  333. cfg = Config()
  334. s3 = S3(cfg)
  335. if len(args) == 0:
  336. raise ParameterError("Nothing to upload. Expecting a local file or directory and a S3 URI destination.")
  337. ## Normalize URI to convert s3://bkt to s3://bkt/ (trailing slash)
  338. destination_base_uri = S3Uri(args.pop())
  339. if destination_base_uri.type != 's3':
  340. raise ParameterError("Destination must be S3Uri. Got: %s" % destination_base_uri)
  341. destination_base = destination_base_uri.uri()
  342. if len(args) == 0:
  343. raise ParameterError("Nothing to upload. Expecting a local file or directory.")
  344. local_list, single_file_local, exclude_list, total_size_local = fetch_local_list(args, is_src = True)
  345. local_count = len(local_list)
  346. info(u"Summary: %d local files to upload" % local_count)
  347. if local_count == 0:
  348. raise ParameterError("Nothing to upload.")
  349. if local_count > 0:
  350. if not single_file_local and '-' in local_list.keys():
  351. raise ParameterError("Cannot specify multiple local files if uploading from '-' (ie stdin)")
  352. elif single_file_local and local_list.keys()[0] == "-" and destination_base.endswith("/"):
  353. raise ParameterError("Destination S3 URI must not end with '/' when uploading from stdin.")
  354. elif not destination_base.endswith("/"):
  355. if not single_file_local:
  356. raise ParameterError("Destination S3 URI must end with '/' (ie must refer to a directory on the remote side).")
  357. local_list[local_list.keys()[0]]['remote_uri'] = destination_base
  358. else:
  359. for key in local_list:
  360. local_list[key]['remote_uri'] = destination_base + key
  361. if cfg.dry_run:
  362. for key in exclude_list:
  363. output(u"exclude: %s" % key)
  364. for key in local_list:
  365. if key != "-":
  366. nicekey = local_list[key]['full_name']
  367. else:
  368. nicekey = "<stdin>"
  369. output(u"upload: '%s' -> '%s'" % (nicekey, local_list[key]['remote_uri']))
  370. warning(u"Exiting now because of --dry-run")
  371. return EX_OK
  372. seq = 0
  373. ret = EX_OK
  374. for key in local_list:
  375. seq += 1
  376. uri_final = S3Uri(local_list[key]['remote_uri'])
  377. try:
  378. src_md5 = local_list.get_md5(key)
  379. except IOError:
  380. src_md5 = None
  381. extra_headers = copy(cfg.extra_headers)
  382. full_name_orig = local_list[key]['full_name']
  383. full_name = full_name_orig
  384. seq_label = "[%d of %d]" % (seq, local_count)
  385. if Config().encrypt:
  386. gpg_exitcode, full_name, extra_headers["x-amz-meta-s3tools-gpgenc"] = gpg_encrypt(full_name_orig)
  387. attr_header = _build_attr_header(local_list[key], key, src_md5)
  388. debug(u"attr_header: %s" % attr_header)
  389. extra_headers.update(attr_header)
  390. try:
  391. response = s3.object_put(full_name, uri_final, extra_headers, extra_label = seq_label)
  392. except S3UploadError as exc:
  393. error(u"Upload of '%s' failed too many times (Last reason: %s)" % (full_name_orig, exc))
  394. if cfg.stop_on_error:
  395. ret = EX_DATAERR
  396. error(u"Exiting now because of --stop-on-error")
  397. break
  398. ret = EX_PARTIAL
  399. continue
  400. except InvalidFileError as exc:
  401. error(u"Upload of '%s' is not possible (Reason: %s)" % (full_name_orig, exc))
  402. ret = EX_PARTIAL
  403. if cfg.stop_on_error:
  404. ret = EX_OSFILE
  405. error(u"Exiting now because of --stop-on-error")
  406. break
  407. continue
  408. if response is not None:
  409. speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
  410. if not Config().progress_meter:
  411. if full_name_orig != "-":
  412. nicekey = full_name_orig
  413. else:
  414. nicekey = "<stdin>"
  415. output(u"upload: '%s' -> '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s) %s" %
  416. (nicekey, uri_final, response["size"], response["elapsed"],
  417. speed_fmt[0], speed_fmt[1], seq_label))
  418. if Config().acl_public:
  419. output(u"Public URL of the object is: %s" %
  420. (uri_final.public_url()))
  421. if Config().encrypt and full_name != full_name_orig:
  422. debug(u"Removing temporary encrypted file: %s" % full_name)
  423. os.remove(deunicodise(full_name))
  424. return ret
  425. def cmd_object_get(args):
  426. cfg = Config()
  427. s3 = S3(cfg)
  428. ## Check arguments:
  429. ## if not --recursive:
  430. ## - first N arguments must be S3Uri
  431. ## - if the last one is S3 make current dir the destination_base
  432. ## - if the last one is a directory:
  433. ## - take all 'basenames' of the remote objects and
  434. ## make the destination name be 'destination_base'+'basename'
  435. ## - if the last one is a file or not existing:
  436. ## - if the number of sources (N, above) == 1 treat it
  437. ## as a filename and save the object there.
  438. ## - if there's more sources -> Error
  439. ## if --recursive:
  440. ## - first N arguments must be S3Uri
  441. ## - for each Uri get a list of remote objects with that Uri as a prefix
  442. ## - apply exclude/include rules
  443. ## - each list item will have MD5sum, Timestamp and pointer to S3Uri
  444. ## used as a prefix.
  445. ## - the last arg may be '-' (stdout)
  446. ## - the last arg may be a local directory - destination_base
  447. ## - if the last one is S3 make current dir the destination_base
  448. ## - if the last one doesn't exist check remote list:
  449. ## - if there is only one item and its_prefix==its_name
  450. ## download that item to the name given in last arg.
  451. ## - if there are more remote items use the last arg as a destination_base
  452. ## and try to create the directory (incl. all parents).
  453. ##
  454. ## In both cases we end up with a list mapping remote object names (keys) to local file names.
  455. ## Each item will be a dict with the following attributes
  456. # {'remote_uri', 'local_filename'}
  457. download_list = []
  458. if len(args) == 0:
  459. raise ParameterError("Nothing to download. Expecting S3 URI.")
  460. if S3Uri(args[-1]).type == 'file':
  461. destination_base = args.pop()
  462. else:
  463. destination_base = "."
  464. if len(args) == 0:
  465. raise ParameterError("Nothing to download. Expecting S3 URI.")
  466. try:
  467. remote_list, exclude_list, remote_total_size = fetch_remote_list(
  468. args, require_attribs = True)
  469. except S3Error as exc:
  470. if exc.code == 'NoSuchKey':
  471. raise ParameterError("Source object '%s' does not exist." % exc.resource)
  472. raise
  473. remote_count = len(remote_list)
  474. info(u"Summary: %d remote files to download" % remote_count)
  475. if remote_count > 0:
  476. if destination_base == "-":
  477. ## stdout is ok for multiple remote files!
  478. for key in remote_list:
  479. remote_list[key]['local_filename'] = "-"
  480. elif not os.path.isdir(deunicodise(destination_base)):
  481. ## We were either given a file name (existing or not)
  482. if remote_count > 1:
  483. raise ParameterError("Destination must be a directory or stdout when downloading multiple sources.")
  484. remote_list[remote_list.keys()[0]]['local_filename'] = destination_base
  485. else:
  486. if destination_base[-1] != os.path.sep:
  487. destination_base += os.path.sep
  488. for key in remote_list:
  489. local_filename = destination_base + key
  490. if os.path.sep != "/":
  491. local_filename = os.path.sep.join(local_filename.split("/"))
  492. remote_list[key]['local_filename'] = local_filename
  493. if cfg.dry_run:
  494. for key in exclude_list:
  495. output(u"exclude: %s" % key)
  496. for key in remote_list:
  497. output(u"download: '%s' -> '%s'" % (remote_list[key]['object_uri_str'], remote_list[key]['local_filename']))
  498. warning(u"Exiting now because of --dry-run")
  499. return EX_OK
  500. seq = 0
  501. ret = EX_OK
  502. for key in remote_list:
  503. seq += 1
  504. item = remote_list[key]
  505. uri = S3Uri(item['object_uri_str'])
  506. ## Encode / Decode destination with "replace" to make sure it's compatible with current encoding
  507. destination = unicodise_safe(item['local_filename'])
  508. seq_label = "[%d of %d]" % (seq, remote_count)
  509. start_position = 0
  510. if destination == "-":
  511. ## stdout
  512. dst_stream = io.open(sys.__stdout__.fileno(), mode='wb', closefd=False)
  513. dst_stream.stream_name = u'<stdout>'
  514. file_exists = True
  515. else:
  516. ## File
  517. try:
  518. file_exists = os.path.exists(deunicodise(destination))
  519. try:
  520. dst_stream = io.open(deunicodise(destination), mode='ab')
  521. dst_stream.stream_name = destination
  522. except IOError as e:
  523. if e.errno != errno.ENOENT:
  524. raise
  525. basename = destination[:destination.rindex(os.path.sep)]
  526. info(u"Creating directory: %s" % basename)
  527. os.makedirs(deunicodise(basename))
  528. dst_stream = io.open(deunicodise(destination), mode='ab')
  529. dst_stream.stream_name = destination
  530. if file_exists:
  531. force = False
  532. skip = False
  533. if Config().get_continue:
  534. start_position = dst_stream.tell()
  535. item_size = item['size']
  536. if start_position == item_size:
  537. skip = True
  538. elif start_position > item_size:
  539. info(u"Download forced for '%s' as source is "
  540. "smaller than local file" % destination)
  541. force = True
  542. elif Config().force:
  543. force = True
  544. elif Config().skip_existing:
  545. skip = True
  546. else:
  547. dst_stream.close()
  548. raise ParameterError(
  549. u"File '%s' already exists. Use either of --force /"
  550. " --continue / --skip-existing or give it a new"
  551. " name." % destination
  552. )
  553. if skip:
  554. dst_stream.close()
  555. info(u"Skipping over existing file: '%s'" % destination)
  556. continue
  557. if force:
  558. start_position = 0
  559. dst_stream.seek(0)
  560. dst_stream.truncate()
  561. except IOError as e:
  562. error(u"Creation of file '%s' failed (Reason: %s)"
  563. % (destination, e.strerror))
  564. if cfg.stop_on_error:
  565. error(u"Exiting now because of --stop-on-error")
  566. raise
  567. ret = EX_PARTIAL
  568. continue
  569. try:
  570. try:
  571. response = s3.object_get(uri, dst_stream, destination, start_position = start_position, extra_label = seq_label)
  572. finally:
  573. dst_stream.close()
  574. except S3DownloadError as e:
  575. error(u"Download of '%s' failed (Reason: %s)" % (destination, e))
  576. # Delete, only if file didn't exist before!
  577. if not file_exists:
  578. debug(u"object_get failed for '%s', deleting..." % (destination,))
  579. os.unlink(deunicodise(destination))
  580. if cfg.stop_on_error:
  581. error(u"Exiting now because of --stop-on-error")
  582. raise
  583. ret = EX_PARTIAL
  584. continue
  585. except S3Error as e:
  586. error(u"Download of '%s' failed (Reason: %s)" % (destination, e))
  587. if not file_exists: # Delete, only if file didn't exist before!
  588. debug(u"object_get failed for '%s', deleting..." % (destination,))
  589. os.unlink(deunicodise(destination))
  590. raise
  591. if "x-amz-meta-s3tools-gpgenc" in response["headers"]:
  592. gpg_decrypt(destination, response["headers"]["x-amz-meta-s3tools-gpgenc"])
  593. response["size"] = os.stat(deunicodise(destination))[6]
  594. if "last-modified" in response["headers"] and destination != "-":
  595. last_modified = time.mktime(time.strptime(response["headers"]["last-modified"], "%a, %d %b %Y %H:%M:%S GMT"))
  596. os.utime(deunicodise(destination), (last_modified, last_modified))
  597. debug("set mtime to %s" % last_modified)
  598. if not Config().progress_meter and destination != "-":
  599. speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
  600. output(u"download: '%s' -> '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s)" %
  601. (uri, destination, response["size"], response["elapsed"], speed_fmt[0], speed_fmt[1]))
  602. if Config().delete_after_fetch:
  603. s3.object_delete(uri)
  604. output(u"File '%s' removed after fetch" % (uri))
  605. return ret
  606. def cmd_object_del(args):
  607. cfg = Config()
  608. recursive = cfg.recursive
  609. for uri_str in args:
  610. uri = S3Uri(uri_str)
  611. if uri.type != "s3":
  612. raise ParameterError("Expecting S3 URI instead of '%s'" % uri_str)
  613. if not uri.has_object():
  614. if recursive and not cfg.force:
  615. raise ParameterError("Please use --force to delete ALL contents of %s" % uri_str)
  616. elif not recursive:
  617. raise ParameterError("File name required, not only the bucket name. Alternatively use --recursive")
  618. if not recursive:
  619. rc = subcmd_object_del_uri(uri_str)
  620. elif cfg.exclude or cfg.include or cfg.max_delete > 0:
  621. # subcmd_batch_del_iterative does not support file exclusion and can't
  622. # accurately know how many total files will be deleted, so revert to batch delete.
  623. rc = subcmd_batch_del(uri_str = uri_str)
  624. else:
  625. rc = subcmd_batch_del_iterative(uri_str = uri_str)
  626. if not rc:
  627. return rc
  628. return EX_OK
  629. def subcmd_batch_del_iterative(uri_str = None, bucket = None):
  630. """ Streaming version of batch deletion (doesn't realize whole list in memory before deleting).
  631. Differences from subcmd_batch_del:
  632. - Does not obey --exclude directives or obey cfg.max_delete (use subcmd_batch_del in those cases)
  633. """
  634. if bucket and uri_str:
  635. raise ValueError("Pass only one of uri_str or bucket")
  636. if bucket: # bucket specified
  637. uri_str = "s3://%s" % bucket
  638. cfg = Config()
  639. s3 = S3(cfg)
  640. uri = S3Uri(uri_str)
  641. bucket = uri.bucket()
  642. deleted_bytes = deleted_count = 0
  643. for _, _, to_delete in s3.bucket_list_streaming(bucket, prefix=uri.object(), recursive=True):
  644. if not to_delete:
  645. continue
  646. if not cfg.dry_run:
  647. response = s3.object_batch_delete_uri_strs([uri.compose_uri(bucket, item['Key']) for item in to_delete])
  648. deleted_bytes += sum(int(item["Size"]) for item in to_delete)
  649. deleted_count += len(to_delete)
  650. output(u'\n'.join(u"delete: '%s'" % uri.compose_uri(bucket, p['Key']) for p in to_delete))
  651. if deleted_count:
  652. # display summary data of deleted files
  653. if cfg.stats:
  654. stats_info = StatsInfo()
  655. stats_info.files_deleted = deleted_count
  656. stats_info.size_deleted = deleted_bytes
  657. output(stats_info.format_output())
  658. else:
  659. total_size, size_coeff = formatSize(deleted_bytes, Config().human_readable_sizes)
  660. total_size_str = str(total_size) + size_coeff
  661. info(u"Deleted %s objects (%s) from %s" % (deleted_count, total_size_str, uri))
  662. else:
  663. warning(u"Remote list is empty.")
  664. return EX_OK
  665. def subcmd_batch_del(uri_str = None, bucket = None, remote_list = None):
  666. """
  667. Returns: EX_OK
  668. Raises: ValueError
  669. """
  670. cfg = Config()
  671. s3 = S3(cfg)
  672. def _batch_del(remote_list):
  673. to_delete = remote_list[:1000]
  674. remote_list = remote_list[1000:]
  675. while len(to_delete):
  676. debug(u"Batch delete %d, remaining %d" % (len(to_delete), len(remote_list)))
  677. if not cfg.dry_run:
  678. response = s3.object_batch_delete(to_delete)
  679. output(u'\n'.join((u"delete: '%s'" % to_delete[p]['object_uri_str']) for p in to_delete))
  680. to_delete = remote_list[:1000]
  681. remote_list = remote_list[1000:]
  682. if remote_list is not None and len(remote_list) == 0:
  683. return False
  684. if len([item for item in [uri_str, bucket, remote_list] if item]) != 1:
  685. raise ValueError("One and only one of 'uri_str', 'bucket', 'remote_list' can be specified.")
  686. if bucket: # bucket specified
  687. uri_str = "s3://%s" % bucket
  688. if remote_list is None: # uri_str specified
  689. remote_list, exclude_list, remote_total_size = fetch_remote_list(uri_str, require_attribs = False)
  690. if len(remote_list) == 0:
  691. warning(u"Remote list is empty.")
  692. return EX_OK
  693. if cfg.max_delete > 0 and len(remote_list) > cfg.max_delete:
  694. warning(u"delete: maximum requested number of deletes would be exceeded, none performed.")
  695. return EX_OK
  696. _batch_del(remote_list)
  697. if cfg.dry_run:
  698. warning(u"Exiting now because of --dry-run")
  699. return EX_OK
  700. def subcmd_object_del_uri(uri_str, recursive = None):
  701. """
  702. Returns: True if XXX, False if XXX
  703. Raises: ValueError
  704. """
  705. cfg = Config()
  706. s3 = S3(cfg)
  707. if recursive is None:
  708. recursive = cfg.recursive
  709. remote_list, exclude_list, remote_total_size = fetch_remote_list(uri_str, require_attribs = False, recursive = recursive)
  710. remote_count = len(remote_list)
  711. info(u"Summary: %d remote files to delete" % remote_count)
  712. if cfg.max_delete > 0 and remote_count > cfg.max_delete:
  713. warning(u"delete: maximum requested number of deletes would be exceeded, none performed.")
  714. return False
  715. if cfg.dry_run:
  716. for key in exclude_list:
  717. output(u"exclude: %s" % key)
  718. for key in remote_list:
  719. output(u"delete: %s" % remote_list[key]['object_uri_str'])
  720. warning(u"Exiting now because of --dry-run")
  721. return True
  722. for key in remote_list:
  723. item = remote_list[key]
  724. response = s3.object_delete(S3Uri(item['object_uri_str']))
  725. output(u"delete: '%s'" % item['object_uri_str'])
  726. return True
  727. def cmd_object_restore(args):
  728. cfg = Config()
  729. s3 = S3(cfg)
  730. if cfg.restore_days < 1:
  731. raise ParameterError("You must restore a file for 1 or more days")
  732. # accept case-insensitive argument but fix it to match S3 API
  733. if cfg.restore_priority.title() not in ['Standard', 'Expedited', 'Bulk']:
  734. raise ParameterError("Valid restoration priorities: bulk, standard, expedited")
  735. else:
  736. cfg.restore_priority = cfg.restore_priority.title()
  737. remote_list, exclude_list, remote_total_size = fetch_remote_list(args, require_attribs = False, recursive = cfg.recursive)
  738. remote_count = len(remote_list)
  739. info(u"Summary: Restoring %d remote files for %d days at %s priority" % (remote_count, cfg.restore_days, cfg.restore_priority))
  740. if cfg.dry_run:
  741. for key in exclude_list:
  742. output(u"exclude: %s" % key)
  743. for key in remote_list:
  744. output(u"restore: '%s'" % remote_list[key]['object_uri_str'])
  745. warning(u"Exiting now because of --dry-run")
  746. return EX_OK
  747. for key in remote_list:
  748. item = remote_list[key]
  749. uri = S3Uri(item['object_uri_str'])
  750. if not item['object_uri_str'].endswith("/"):
  751. try:
  752. response = s3.object_restore(S3Uri(item['object_uri_str']))
  753. output(u"restore: '%s'" % item['object_uri_str'])
  754. except S3Error as e:
  755. if e.code == "RestoreAlreadyInProgress":
  756. warning("%s: %s" % (e.message, item['object_uri_str']))
  757. else:
  758. raise e
  759. else:
  760. debug(u"Skipping directory since only files may be restored")
  761. return EX_OK
  762. def subcmd_cp_mv(args, process_fce, action_str, message):
  763. cfg = Config()
  764. if action_str == 'modify':
  765. if len(args) < 1:
  766. raise ParameterError("Expecting one or more S3 URIs for "
  767. + action_str)
  768. destination_base = None
  769. else:
  770. if len(args) < 2:
  771. raise ParameterError("Expecting two or more S3 URIs for "
  772. + action_str)
  773. dst_base_uri = S3Uri(args.pop())
  774. if dst_base_uri.type != "s3":
  775. raise ParameterError("Destination must be S3 URI. To download a "
  776. "file use 'get' or 'sync'.")
  777. destination_base = dst_base_uri.uri()
  778. scoreboard = ExitScoreboard()
  779. remote_list, exclude_list, remote_total_size = \
  780. fetch_remote_list(args, require_attribs=False)
  781. remote_count = len(remote_list)
  782. info(u"Summary: %d remote files to %s" % (remote_count, action_str))
  783. if destination_base:
  784. # Trying to mv dir1/ to dir2 will not pass a test in S3.FileLists,
  785. # so we don't need to test for it here.
  786. if not destination_base.endswith('/') \
  787. and (len(remote_list) > 1 or cfg.recursive):
  788. raise ParameterError("Destination must be a directory and end with"
  789. " '/' when acting on a folder content or on "
  790. "multiple sources.")
  791. if cfg.recursive:
  792. for key in remote_list:
  793. remote_list[key]['dest_name'] = destination_base + key
  794. else:
  795. for key in remote_list:
  796. if destination_base.endswith("/"):
  797. remote_list[key]['dest_name'] = destination_base + key
  798. else:
  799. remote_list[key]['dest_name'] = destination_base
  800. else:
  801. for key in remote_list:
  802. remote_list[key]['dest_name'] = remote_list[key]['object_uri_str']
  803. if cfg.dry_run:
  804. for key in exclude_list:
  805. output(u"exclude: %s" % key)
  806. for key in remote_list:
  807. output(u"%s: '%s' -> '%s'" % (action_str,
  808. remote_list[key]['object_uri_str'],
  809. remote_list[key]['dest_name']))
  810. warning(u"Exiting now because of --dry-run")
  811. return EX_OK
  812. seq = 0
  813. for key in remote_list:
  814. seq += 1
  815. seq_label = "[%d of %d]" % (seq, remote_count)
  816. item = remote_list[key]
  817. src_uri = S3Uri(item['object_uri_str'])
  818. dst_uri = S3Uri(item['dest_name'])
  819. src_size = item.get('size')
  820. extra_headers = copy(cfg.extra_headers)
  821. try:
  822. response = process_fce(src_uri, dst_uri, extra_headers,
  823. src_size=src_size,
  824. extra_label=seq_label)
  825. output(message % {"src": src_uri, "dst": dst_uri,
  826. "extra": seq_label})
  827. if Config().acl_public:
  828. info(u"Public URL is: %s" % dst_uri.public_url())
  829. scoreboard.success()
  830. except (S3Error, S3UploadError) as exc:
  831. if isinstance(exc, S3Error) and exc.code == "NoSuchKey":
  832. scoreboard.notfound()
  833. warning(u"Key not found %s" % item['object_uri_str'])
  834. else:
  835. scoreboard.failed()
  836. error(u"Copy failed for: '%s' (%s)", item['object_uri_str'],
  837. exc)
  838. if cfg.stop_on_error:
  839. break
  840. return scoreboard.rc()
  841. def cmd_cp(args):
  842. s3 = S3(Config())
  843. return subcmd_cp_mv(args, s3.object_copy, "copy",
  844. u"remote copy: '%(src)s' -> '%(dst)s' %(extra)s")
  845. def cmd_modify(args):
  846. s3 = S3(Config())
  847. return subcmd_cp_mv(args, s3.object_modify, "modify",
  848. u"modify: '%(src)s' %(extra)s")
  849. def cmd_mv(args):
  850. s3 = S3(Config())
  851. return subcmd_cp_mv(args, s3.object_move, "move",
  852. u"move: '%(src)s' -> '%(dst)s' %(extra)s")
  853. def cmd_info(args):
  854. cfg = Config()
  855. s3 = S3(cfg)
  856. while (len(args)):
  857. uri_arg = args.pop(0)
  858. uri = S3Uri(uri_arg)
  859. if uri.type != "s3" or not uri.has_bucket():
  860. raise ParameterError("Expecting S3 URI instead of '%s'" % uri_arg)
  861. try:
  862. if uri.has_object():
  863. info = s3.object_info(uri)
  864. output(u"%s (object):" % uri.uri())
  865. output(u" File size: %s" % info['headers']['content-length'])
  866. output(u" Last mod: %s" % info['headers']['last-modified'])
  867. output(u" MIME type: %s" % info['headers'].get('content-type', 'none'))
  868. output(u" Storage: %s" % info['headers'].get('x-amz-storage-class', 'STANDARD'))
  869. md5 = info['headers'].get('etag', '').strip('"\'')
  870. try:
  871. md5 = info['s3cmd-attrs']['md5']
  872. except KeyError:
  873. pass
  874. output(u" MD5 sum: %s" % md5)
  875. if 'x-amz-server-side-encryption' in info['headers']:
  876. output(u" SSE: %s" % info['headers']['x-amz-server-side-encryption'])
  877. else:
  878. output(u" SSE: none")
  879. else:
  880. info = s3.bucket_info(uri)
  881. output(u"%s (bucket):" % uri.uri())
  882. output(u" Location: %s" % (info['bucket-location']
  883. or 'none'))
  884. output(u" Payer: %s" % (info['requester-pays']
  885. or 'none'))
  886. expiration = s3.expiration_info(uri, cfg.bucket_location)
  887. if expiration and expiration['prefix'] is not None:
  888. expiration_desc = "Expiration Rule: "
  889. if expiration['prefix'] == "":
  890. expiration_desc += "all objects in this bucket "
  891. elif expiration['prefix'] is not None:
  892. expiration_desc += "objects with key prefix '" + expiration['prefix'] + "' "
  893. expiration_desc += "will expire in '"
  894. if expiration['days']:
  895. expiration_desc += expiration['days'] + "' day(s) after creation"
  896. elif expiration['date']:
  897. expiration_desc += expiration['date'] + "' "
  898. output(u" %s" % expiration_desc)
  899. else:
  900. output(u" Expiration Rule: none")
  901. try:
  902. policy = s3.get_policy(uri)
  903. output(u" Policy: %s" % policy)
  904. except S3Error as exc:
  905. # Ignore the exception and don't fail the info
  906. # if the server doesn't support setting ACLs
  907. if exc.status == 403:
  908. output(u" Policy: Not available: GetPolicy permission is needed to read the policy")
  909. elif exc.status == 405:
  910. output(u" Policy: Not available: Only the bucket owner can read the policy")
  911. elif exc.status not in [404, 501]:
  912. raise exc
  913. else:
  914. output(u" Policy: none")
  915. try:
  916. cors = s3.get_cors(uri)
  917. output(u" CORS: %s" % cors)
  918. except S3Error as exc:
  919. # Ignore the exception and don't fail the info
  920. # if the server doesn't support setting ACLs
  921. if exc.status not in [404, 501]:
  922. raise exc
  923. output(u" CORS: none")
  924. try:
  925. acl = s3.get_acl(uri)
  926. acl_grant_list = acl.getGrantList()
  927. for grant in acl_grant_list:
  928. output(u" ACL: %s: %s" % (grant['grantee'], grant['permission']))
  929. if acl.isAnonRead():
  930. output(u" URL: %s" % uri.public_url())
  931. except S3Error as exc:
  932. # Ignore the exception and don't fail the info
  933. # if the server doesn't support setting ACLs
  934. if exc.status not in [404, 501]:
  935. raise exc
  936. else:
  937. output(u" ACL: none")
  938. if uri.has_object():
  939. # Temporary hack for performance + python3 compatibility
  940. if PY3:
  941. info_headers_iter = info['headers'].items()
  942. else:
  943. info_headers_iter = info['headers'].iteritems()
  944. for header, value in info_headers_iter:
  945. if header.startswith('x-amz-meta-'):
  946. output(u" %s: %s" % (header, value))
  947. except S3Error as e:
  948. if e.info["Code"] in S3.codes:
  949. error(S3.codes[e.info["Code"]] % uri.bucket())
  950. raise
  951. return EX_OK
  952. def filedicts_to_keys(*args):
  953. keys = set()
  954. for a in args:
  955. keys.update(a.keys())
  956. keys = list(keys)
  957. keys.sort()
  958. return keys
  959. def cmd_sync_remote2remote(args):
  960. cfg = Config()
  961. s3 = S3(cfg)
  962. # Normalise s3://uri (e.g. assert trailing slash)
  963. destination_base = S3Uri(args[-1]).uri()
  964. destbase_with_source_list = set()
  965. for source_arg in args[:-1]:
  966. if source_arg.endswith('/'):
  967. destbase_with_source_list.add(destination_base)
  968. else:
  969. destbase_with_source_list.add(os.path.join(destination_base,
  970. os.path.basename(source_arg)))
  971. stats_info = StatsInfo()
  972. src_list, src_exclude_list, remote_total_size = fetch_remote_list(args[:-1], recursive = True, require_attribs = True)
  973. dst_list, dst_exclude_list, _ = fetch_remote_list(destbase_with_source_list, recursive = True, require_attribs = True)
  974. src_count = len(src_list)
  975. orig_src_count = src_count
  976. dst_count = len(dst_list)
  977. deleted_count = 0
  978. info(u"Found %d source files, %d destination files" % (src_count, dst_count))
  979. src_list, dst_list, update_list, copy_pairs = compare_filelists(src_list, dst_list, src_remote = True, dst_remote = True)
  980. src_count = len(src_list)
  981. update_count = len(update_list)
  982. dst_count = len(dst_list)
  983. print(u"Summary: %d source files to copy, %d files at destination to delete" % (src_count + update_count, dst_count))
  984. ### Populate 'target_uri' only if we've got something to sync from src to dst
  985. for key in src_list:
  986. src_list[key]['target_uri'] = destination_base + key
  987. for key in update_list:
  988. update_list[key]['target_uri'] = destination_base + key
  989. if cfg.dry_run:
  990. keys = filedicts_to_keys(src_exclude_list, dst_exclude_list)
  991. for key in keys:
  992. output(u"exclude: %s" % key)
  993. if cfg.delete_removed:
  994. for key in dst_list:
  995. output(u"delete: '%s'" % dst_list[key]['object_uri_str'])
  996. for key in src_list:
  997. output(u"remote copy: '%s' -> '%s'" % (src_list[key]['object_uri_str'], src_list[key]['target_uri']))
  998. for key in update_list:
  999. output(u"remote copy: '%s' -> '%s'" % (update_list[key]['object_uri_str'], update_list[key]['target_uri']))
  1000. warning(u"Exiting now because of --dry-run")
  1001. return EX_OK
  1002. # if there are copy pairs, we can't do delete_before, on the chance
  1003. # we need one of the to-be-deleted files as a copy source.
  1004. if len(copy_pairs) > 0:
  1005. cfg.delete_after = True
  1006. if cfg.delete_removed and orig_src_count == 0 and len(dst_list) and not cfg.force:
  1007. warning(u"delete: cowardly refusing to delete because no source files were found. Use --force to override.")
  1008. cfg.delete_removed = False
  1009. # Delete items in destination that are not in source
  1010. if cfg.delete_removed and not cfg.delete_after:
  1011. subcmd_batch_del(remote_list = dst_list)
  1012. deleted_count = len(dst_list)
  1013. def _upload(src_list, seq, src_count):
  1014. file_list = src_list.keys()
  1015. file_list.sort()
  1016. ret = EX_OK
  1017. total_nb_files = 0
  1018. total_size = 0
  1019. for file in file_list:
  1020. seq += 1
  1021. item = src_list[file]
  1022. src_uri = S3Uri(item['object_uri_str'])
  1023. dst_uri = S3Uri(item['target_uri'])
  1024. src_size = item.get('size')
  1025. seq_label = "[%d of %d]" % (seq, src_count)
  1026. extra_headers = copy(cfg.extra_headers)
  1027. try:
  1028. response = s3.object_copy(src_uri, dst_uri, extra_headers,
  1029. src_size=src_size,
  1030. extra_label=seq_label)
  1031. output(u"remote copy: '%s' -> '%s' %s" %
  1032. (src_uri, dst_uri, seq_label))
  1033. total_nb_files += 1
  1034. total_size += item.get(u'size', 0)
  1035. except (S3Error, S3UploadError) as exc:
  1036. ret = EX_PARTIAL
  1037. error(u"File '%s' could not be copied: %s", src_uri, exc)
  1038. if cfg.stop_on_error:
  1039. raise
  1040. return ret, seq, total_nb_files, total_size
  1041. # Perform the synchronization of files
  1042. timestamp_start = time.time()
  1043. seq = 0
  1044. ret, seq, nb_files, size = _upload(src_list, seq, src_count + update_count)
  1045. total_files_copied = nb_files
  1046. total_size_copied = size
  1047. status, seq, nb_files, size = _upload(update_list, seq, src_count + update_count)
  1048. if ret == EX_OK:
  1049. ret = status
  1050. total_files_copied += nb_files
  1051. total_size_copied += size
  1052. n_copied, bytes_saved, failed_copy_files = remote_copy(
  1053. s3, copy_pairs, destination_base, None, False)
  1054. total_files_copied += n_copied
  1055. total_size_copied += bytes_saved
  1056. #process files not copied
  1057. debug("Process files that were not remotely copied")
  1058. failed_copy_count = len(failed_copy_files)
  1059. for key in failed_copy_files:
  1060. failed_copy_files[key]['target_uri'] = destination_base + key
  1061. status, seq, nb_files, size = _upload(failed_copy_files, seq, src_count + update_count + failed_copy_count)
  1062. if ret == EX_OK:
  1063. ret = status
  1064. total_files_copied += nb_files
  1065. total_size_copied += size
  1066. # Delete items in destination that are not in source
  1067. if cfg.delete_removed and cfg.delete_after:
  1068. subcmd_batch_del(remote_list = dst_list)
  1069. deleted_count = len(dst_list)
  1070. stats_info.files = orig_src_count
  1071. stats_info.size = remote_total_size
  1072. stats_info.files_copied = total_files_copied
  1073. stats_info.size_copied = total_size_copied
  1074. stats_info.files_deleted = deleted_count
  1075. total_elapsed = max(1.0, time.time() - timestamp_start)
  1076. outstr = "Done. Copied %d files in %0.1f seconds, %0.2f files/s." % (total_files_copied, total_elapsed, seq / total_elapsed)
  1077. if cfg.stats:
  1078. outstr += stats_info.format_output()
  1079. output(outstr)
  1080. elif seq > 0:
  1081. output(outstr)
  1082. else:
  1083. info(outstr)
  1084. return ret
  1085. def cmd_sync_remote2local(args):
  1086. cfg = Config()
  1087. s3 = S3(cfg)
  1088. def _do_deletes(local_list):
  1089. total_size = 0
  1090. if cfg.max_delete > 0 and len(local_list) > cfg.max_delete:
  1091. warning(u"delete: maximum requested number of deletes would be exceeded, none performed.")
  1092. return total_size
  1093. for key in local_list:
  1094. os.unlink(deunicodise(local_list[key]['full_name']))
  1095. output(u"delete: '%s'" % local_list[key]['full_name'])
  1096. total_size += local_list[key].get(u'size', 0)
  1097. return len(local_list), total_size
  1098. destination_base = args[-1]
  1099. source_args = args[:-1]
  1100. fetch_source_args = args[:-1]
  1101. if not destination_base.endswith(os.path.sep):
  1102. if fetch_source_args[0].endswith(u'/') or len(fetch_source_args) > 1:
  1103. raise ParameterError("Destination must be a directory and end with '/' when downloading multiple sources.")
  1104. stats_info = StatsInfo()
  1105. remote_list, src_exclude_list, remote_total_size = fetch_remote_list(fetch_source_args, recursive = True, require_attribs = True)
  1106. # - The source path is either like "/myPath/my_src_folder" and
  1107. # the user want to download this single folder and Optionally only delete
  1108. # things that have been removed inside this folder. For this case, we only
  1109. # have to look inside destination_base/my_src_folder and not at the root of
  1110. # destination_base.
  1111. # - Or like "/myPath/my_src_folder/" and the user want to have the sync
  1112. # with the content of this folder
  1113. destbase_with_source_list = set()
  1114. for source_arg in fetch_source_args:
  1115. if source_arg.endswith('/'):
  1116. if destination_base.endswith(os.path.sep):
  1117. destbase_with_source_list.add(destination_base)
  1118. else:
  1119. destbase_with_source_list.add(destination_base + os.path.sep)
  1120. else:
  1121. destbase_with_source_list.add(os.path.join(destination_base,
  1122. os.path.basename(source_arg)))
  1123. local_list, single_file_local, dst_exclude_list, local_total_size = fetch_local_list(destbase_with_source_list, is_src = False, recursive = True)
  1124. local_count = len(local_list)
  1125. remote_count = len(remote_list)
  1126. orig_remote_count = remote_count
  1127. info(u"Found %d remote files, %d local files" % (remote_count, local_count))
  1128. remote_list, local_list, update_list, copy_pairs = compare_filelists(remote_list, local_list, src_remote = True, dst_remote = False)
  1129. local_count = len(local_list)
  1130. remote_count = len(remote_list)
  1131. update_count = len(update_list)
  1132. copy_pairs_count = len(copy_pairs)
  1133. info(u"Summary: %d remote files to download, %d local files to delete, %d local files to hardlink" % (remote_count + update_count, local_count, copy_pairs_count))
  1134. def _set_local_filename(remote_list, destination_base, source_args):
  1135. if len(remote_list) == 0:
  1136. return
  1137. if destination_base.endswith(os.path.sep):
  1138. if not os.path.exists(deunicodise(destination_base)):
  1139. if not cfg.dry_run:
  1140. os.makedirs(deunicodise(destination_base))
  1141. if not os.path.isdir(deunicodise(destination_base)):
  1142. raise ParameterError("Destination is not an existing directory")
  1143. elif len(remote_list) == 1 and \
  1144. source_args[0] == remote_list[remote_list.keys()[0]].get(u'object_uri_str', ''):
  1145. if os.path.isdir(deunicodise(destination_base)):
  1146. raise ParameterError("Destination already exists and is a directory")
  1147. remote_list[remote_list.keys()[0]]['local_filename'] = destination_base
  1148. return
  1149. if destination_base[-1] != os.path.sep:
  1150. destination_base += os.path.sep
  1151. for key in remote_list:
  1152. local_filename = destination_base + key
  1153. if os.path.sep != "/":
  1154. local_filename = os.path.sep.join(local_filename.split("/"))
  1155. remote_list[key]['local_filename'] = local_filename
  1156. _set_local_filename(remote_list, destination_base, source_args)
  1157. _set_local_filename(update_list, destination_base, source_args)
  1158. if cfg.dry_run:
  1159. keys = filedicts_to_keys(src_exclude_list, dst_exclude_list)
  1160. for key in keys:
  1161. output(u"exclude: %s" % key)
  1162. if cfg.delete_removed:
  1163. for key in local_list:
  1164. output(u"delete: '%s'" % local_list[key]['full_name'])
  1165. for key in remote_list:
  1166. output(u"download: '%s' -> '%s'" % (remote_list[key]['object_uri_str'], remote_list[key]['local_filename']))
  1167. for key in update_list:
  1168. output(u"download: '%s' -> '%s'" % (update_list[key]['object_uri_str'], update_list[key]['local_filename']))
  1169. warning(u"Exiting now because of --dry-run")
  1170. return EX_OK
  1171. # if there are copy pairs, we can't do delete_before, on the chance
  1172. # we need one of the to-be-deleted files as a copy source.
  1173. if len(copy_pairs) > 0:
  1174. cfg.delete_after = True
  1175. if cfg.delete_removed and orig_remote_count == 0 and len(local_list) and not cfg.force:
  1176. warning(u"delete: cowardly refusing to delete because no source files were found. Use --force to override.")
  1177. cfg.delete_removed = False
  1178. if cfg.delete_removed and not cfg.delete_after:
  1179. deleted_count, deleted_size = _do_deletes(local_list)
  1180. else:
  1181. deleted_count, deleted_size = (0, 0)
  1182. def _download(remote_list, seq, total, total_size, dir_cache):
  1183. original_umask = os.umask(0)
  1184. os.umask(original_umask)
  1185. file_list = remote_list.keys()
  1186. file_list.sort()
  1187. ret = EX_OK
  1188. for file in file_list:
  1189. seq += 1
  1190. item = remote_list[file]
  1191. uri = S3Uri(item['object_uri_str'])
  1192. dst_file = item['local_filename']
  1193. is_empty_directory = dst_file.endswith('/')
  1194. seq_label = "[%d of %d]" % (seq, total)
  1195. dst_dir = unicodise(os.path.dirname(deunicodise(dst_file)))
  1196. if not dst_dir in dir_cache:
  1197. dir_cache[dst_dir] = Utils.mkdir_with_parents(dst_dir)
  1198. if dir_cache[dst_dir] == False:
  1199. if cfg.stop_on_error:
  1200. error(u"Exiting now because of --stop-on-error")
  1201. raise OSError("Download of '%s' failed (Reason: %s destination directory is not writable)" % (file, dst_dir))
  1202. error(u"Download of '%s' failed (Reason: %s destination directory is not writable)" % (file, dst_dir))
  1203. ret = EX_PARTIAL
  1204. continue
  1205. try:
  1206. chkptfname_b = ''
  1207. if not is_empty_directory: # ignore empty directory at S3:
  1208. debug(u"dst_file=%s" % dst_file)
  1209. # create temporary files (of type .s3cmd.XXXX.tmp) in the same directory
  1210. # for downloading and then rename once downloaded
  1211. # unicode provided to mkstemp argument
  1212. chkptfd, chkptfname_b = tempfile.mkstemp(u".tmp", u".s3cmd.",
  1213. os.path.dirname(dst_file))
  1214. with io.open(chkptfd, mode='wb') as dst_stream:
  1215. dst_stream.stream_name = unicodise(chkptfname_b)
  1216. debug(u"created chkptfname=%s" % dst_stream.stream_name)
  1217. response = s3.object_get(uri, dst_stream, dst_file, extra_label = seq_label)
  1218. # download completed, rename the file to destination
  1219. if os.name == "nt":
  1220. # Windows is really a bad OS. Rename can't overwrite an existing file
  1221. try:
  1222. os.unlink(deunicodise(dst_file))
  1223. except OSError:
  1224. pass
  1225. os.rename(chkptfname_b, deunicodise(dst_file))
  1226. debug(u"renamed chkptfname=%s to dst_file=%s" % (dst_stream.stream_name, dst_file))
  1227. except OSError as exc:
  1228. allow_partial = True
  1229. if exc.errno == errno.EISDIR:
  1230. error(u"Download of '%s' failed (Reason: %s is a directory)" % (file, dst_file))
  1231. elif os.name != "nt" and exc.errno == errno.ETXTBSY:
  1232. error(u"Download of '%s' failed (Reason: %s is currently open for execute, cannot be overwritten)" % (file, dst_file))
  1233. elif exc.errno == errno.EPERM or exc.errno == errno.EACCES:
  1234. error(u"Download of '%s' failed (Reason: %s permission denied)" % (file, dst_file))
  1235. elif exc.errno == errno.EBUSY:
  1236. error(u"Download of '%s' failed (Reason: %s is busy)" % (file, dst_file))
  1237. elif exc.errno == errno.EFBIG:
  1238. error(u"Download of '%s' failed (Reason: %s is too big)" % (file, dst_file))
  1239. elif exc.errno == errno.ENAMETOOLONG:
  1240. error(u"Download of '%s' failed (Reason: File Name is too long)" % file)
  1241. elif (exc.errno == errno.ENOSPC or (os.name != "nt" and exc.errno == errno.EDQUOT)):
  1242. error(u"Download of '%s' failed (Reason: No space left)" % file)
  1243. allow_partial = False
  1244. else:
  1245. error(u"Download of '%s' failed (Reason: Unknown OsError %d)" % (file, exc.errno or 0))
  1246. allow_partial = False
  1247. try:
  1248. # Try to remove the temp file if it exists
  1249. if chkptfname_b:
  1250. os.unlink(chkptfname_b)
  1251. except Exception:
  1252. pass
  1253. if allow_partial and not cfg.stop_on_error:
  1254. ret = EX_PARTIAL
  1255. continue
  1256. ret = EX_OSFILE
  1257. if allow_partial:
  1258. error(u"Exiting now because of --stop-on-error")
  1259. else:
  1260. error(u"Exiting now because of fatal error")
  1261. raise
  1262. except S3DownloadError as exc:
  1263. error(u"Download of '%s' failed too many times (Last Reason: %s). "
  1264. "This is usually a transient error, please try again "
  1265. "later." % (file, exc))
  1266. try:
  1267. os.unlink(chkptfname_b)
  1268. except Exception as sub_exc:
  1269. warning(u"Error deleting temporary file %s (Reason: %s)",
  1270. (dst_stream.stream_name, sub_exc))
  1271. if cfg.stop_on_error:
  1272. ret = EX_DATAERR
  1273. error(u"Exiting now because of --stop-on-error")
  1274. raise
  1275. ret = EX_PARTIAL
  1276. continue
  1277. except S3Error as exc:
  1278. warning(u"Remote file '%s'. S3Error: %s" % (exc.resource, exc))
  1279. try:
  1280. os.unlink(chkptfname_b)
  1281. except Exception as sub_exc:
  1282. warning(u"Error deleting temporary file %s (Reason: %s)",
  1283. (dst_stream.stream_name, sub_exc))
  1284. if cfg.stop_on_error:
  1285. raise
  1286. ret = EX_PARTIAL
  1287. continue
  1288. try:
  1289. # set permissions on destination file
  1290. if not is_empty_directory: # a normal file
  1291. mode = 0o777 - original_umask
  1292. else:
  1293. # an empty directory, make them readable/executable
  1294. mode = 0o775
  1295. debug(u"mode=%s" % oct(mode))
  1296. os.chmod(deunicodise(dst_file), mode)
  1297. except:
  1298. raise
  1299. # because we don't upload empty directories,
  1300. # we can continue the loop here, we won't be setting stat info.
  1301. # if we do start to upload empty directories, we'll have to reconsider this.
  1302. if is_empty_directory:
  1303. continue
  1304. try:
  1305. if 's3cmd-attrs' in response and cfg.preserve_attrs:
  1306. attrs = response['s3cmd-attrs']
  1307. if 'mode' in attrs:
  1308. os.chmod(deunicodise(dst_file), int(attrs['mode']))
  1309. if 'mtime' in attrs or 'atime' in attrs:
  1310. mtime = ('mtime' in attrs) and int(attrs['mtime']) or int(time.time())
  1311. atime = ('atime' in attrs) and int(attrs['atime']) or int(time.time())
  1312. os.utime(deunicodise(dst_file), (atime, mtime))
  1313. if 'uid' in attrs and 'gid' in attrs:
  1314. uid = int(attrs['uid'])
  1315. gid = int(attrs['gid'])
  1316. os.lchown(deunicodise(dst_file),uid,gid)
  1317. elif 'last-modified' in response['headers']:
  1318. last_modified = time.mktime(time.strptime(response["headers"]["last-modified"], "%a, %d %b %Y %H:%M:%S GMT"))
  1319. os.utime(deunicodise(dst_file), (last_modified, last_modified))
  1320. debug("set mtime to %s" % last_modified)
  1321. except OSError as e:
  1322. ret = EX_PARTIAL
  1323. if e.errno == errno.EEXIST:
  1324. warning(u"%s exists - not overwriting" % dst_file)
  1325. continue
  1326. if e.errno in (errno.EPERM, errno.EACCES):
  1327. warning(u"%s not writable: %s" % (dst_file, e.strerror))
  1328. if cfg.stop_on_error:
  1329. raise e
  1330. continue
  1331. raise e
  1332. except KeyboardInterrupt:
  1333. warning(u"Exiting after keyboard interrupt")
  1334. return
  1335. except Exception as e:
  1336. ret = EX_PARTIAL
  1337. error(u"%s: %s" % (file, e))
  1338. if cfg.stop_on_error:
  1339. raise OSError(e)
  1340. continue
  1341. finally:
  1342. try:
  1343. os.remove(chkptfname_b)
  1344. except Exception:
  1345. pass
  1346. speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
  1347. if not Config().progress_meter:
  1348. output(u"download: '%s' -> '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s) %s" %
  1349. (uri, dst_file, response["size"], response["elapsed"], speed_fmt[0], speed_fmt[1],
  1350. seq_label))
  1351. total_size += response["size"]
  1352. if Config().delete_after_fetch:
  1353. s3.object_delete(uri)
  1354. output(u"File '%s' removed after syncing" % (uri))
  1355. return ret, seq, total_size
  1356. size_transferred = 0
  1357. total_elapsed = 0.0
  1358. timestamp_start = time.time()
  1359. dir_cache = {}
  1360. seq = 0
  1361. ret, seq, size_transferred = _download(remote_list, seq, remote_count + update_count, size_transferred, dir_cache)
  1362. status, seq, size_transferred = _download(update_list, seq, remote_count + update_count, size_transferred, dir_cache)
  1363. if ret == EX_OK:
  1364. ret = status
  1365. n_copies, size_copies, failed_copy_list = local_copy(copy_pairs, destination_base)
  1366. _set_local_filename(failed_copy_list, destination_base, source_args)
  1367. status, seq, size_transferred = _download(failed_copy_list, seq, len(failed_copy_list) + remote_count + update_count, size_transferred, dir_cache)
  1368. if ret == EX_OK:
  1369. ret = status
  1370. if cfg.delete_removed and cfg.delete_after:
  1371. deleted_count, deleted_size = _do_deletes(local_list)
  1372. total_elapsed = max(1.0, time.time() - timestamp_start)
  1373. speed_fmt = formatSize(size_transferred / total_elapsed, human_readable = True, floating_point = True)
  1374. stats_info.files = orig_remote_count
  1375. stats_info.size = remote_total_size
  1376. stats_info.files_transferred = len(failed_copy_list) + remote_count + update_count
  1377. stats_info.size_transferred = size_transferred
  1378. stats_info.files_copied = n_copies
  1379. stats_info.size_copied = size_copies
  1380. stats_info.files_deleted = deleted_count
  1381. stats_info.size_deleted = deleted_size
  1382. # Only print out the result if any work has been done or
  1383. # if the user asked for verbose output
  1384. outstr = "Done. Downloaded %d bytes in %0.1f seconds, %0.2f %sB/s." % (size_transferred, total_elapsed, speed_fmt[0], speed_fmt[1])
  1385. if cfg.stats:
  1386. outstr += stats_info.format_output()
  1387. output(outstr)
  1388. elif size_transferred > 0:
  1389. output(outstr)
  1390. else:
  1391. info(outstr)
  1392. return ret
  1393. def local_copy(copy_pairs, destination_base):
  1394. # Do NOT hardlink local files by default, that'd be silly
  1395. # For instance all empty files would become hardlinked together!
  1396. saved_bytes = 0
  1397. failed_copy_list = FileDict()
  1398. for (src_obj, dst1, relative_file, md5) in copy_pairs:
  1399. src_file = os.path.join(destination_base, dst1)
  1400. dst_file = os.path.join(destination_base, relative_file)
  1401. dst_dir = os.path.dirname(deunicodise(dst_file))
  1402. try:
  1403. if not os.path.isdir(deunicodise(dst_dir)):
  1404. debug("MKDIR %s" % dst_dir)
  1405. os.makedirs(deunicodise(dst_dir))
  1406. debug(u"Copying %s to %s" % (src_file, dst_file))
  1407. shutil.copy2(deunicodise(src_file), deunicodise(dst_file))
  1408. saved_bytes += src_obj.get(u'size', 0)
  1409. except (IOError, OSError) as e:
  1410. warning(u'Unable to copy or hardlink files %s -> %s (Reason: %s)' % (src_file, dst_file, e))
  1411. failed_copy_list[relative_file] = src_obj
  1412. return len(copy_pairs), saved_bytes, failed_copy_list
  1413. def remote_copy(s3, copy_pairs, destination_base, uploaded_objects_list=None,
  1414. metadata_update=False):
  1415. cfg = Config()
  1416. saved_bytes = 0
  1417. failed_copy_list = FileDict()
  1418. seq = 0
  1419. src_count = len(copy_pairs)
  1420. for (src_obj, dst1, dst2, src_md5) in copy_pairs:
  1421. seq += 1
  1422. debug(u"Remote Copying from %s to %s" % (dst1, dst2))
  1423. dst1_uri = S3Uri(destination_base + dst1)
  1424. dst2_uri = S3Uri(destination_base + dst2)
  1425. src_obj_size = src_obj.get(u'size', 0)
  1426. seq_label = "[%d of %d]" % (seq, src_count)
  1427. extra_headers = copy(cfg.extra_headers)
  1428. if metadata_update:
  1429. # source is a real local file with its own personal metadata
  1430. attr_header = _build_attr_header(src_obj, dst2, src_md5)
  1431. debug(u"attr_header: %s" % attr_header)
  1432. extra_headers.update(attr_header)
  1433. extra_headers['content-type'] = \
  1434. s3.content_type(filename=src_obj['full_name'])
  1435. try:
  1436. s3.object_copy(dst1_uri, dst2_uri, extra_headers,
  1437. src_size=src_obj_size,
  1438. extra_label=seq_label)
  1439. output(u"remote copy: '%s' -> '%s' %s" % (dst1, dst2, seq_label))
  1440. saved_bytes += src_obj_size
  1441. if uploaded_objects_list is not None:
  1442. uploaded_objects_list.append(dst2)
  1443. except Exception:
  1444. warning(u"Unable to remote copy files '%s' -> '%s'" % (dst1_uri, dst2_uri))
  1445. failed_copy_list[dst2] = src_obj
  1446. return (len(copy_pairs), saved_bytes, failed_copy_list)
  1447. def _build_attr_header(src_obj, src_relative_name, md5=None):
  1448. cfg = Config()
  1449. attrs = {}
  1450. if cfg.preserve_attrs:
  1451. for attr in cfg.preserve_attrs_list:
  1452. val = None
  1453. if attr == 'uname':
  1454. try:
  1455. val = Utils.urlencode_string(Utils.getpwuid_username(src_obj['uid']), unicode_output=True)
  1456. except (KeyError, TypeError):
  1457. attr = "uid"
  1458. val = src_obj.get('uid')
  1459. if val:
  1460. warning(u"%s: Owner username not known. Storing UID=%d instead." % (src_relative_name, val))
  1461. elif attr == 'gname':
  1462. try:
  1463. val = Utils.urlencode_string(Utils.getgrgid_grpname(src_obj.get('gid')), unicode_output=True)
  1464. except (KeyError, TypeError):
  1465. attr = "gid"
  1466. val = src_obj.get('gid')
  1467. if val:
  1468. warning(u"%s: Owner groupname not known. Storing GID=%d instead." % (src_relative_name, val))
  1469. elif attr != "md5":
  1470. try:
  1471. val = getattr(src_obj['sr'], 'st_' + attr)
  1472. except Exception:
  1473. val = None
  1474. if val is not None:
  1475. attrs[attr] = val
  1476. if 'md5' in cfg.preserve_attrs_list and md5:
  1477. attrs['md5'] = md5
  1478. if attrs:
  1479. attr_str_list = []
  1480. for k in sorted(attrs.keys()):
  1481. attr_str_list.append(u"%s:%s" % (k, attrs[k]))
  1482. attr_header = {'x-amz-meta-s3cmd-attrs': u'/'.join(attr_str_list)}
  1483. else:
  1484. attr_header = {}
  1485. return attr_header
  1486. def cmd_sync_local2remote(args):
  1487. cfg = Config()
  1488. s3 = S3(cfg)
  1489. def _single_process(source_args):
  1490. for dest in destinations:
  1491. ## Normalize URI to convert s3://bkt to s3://bkt/ (trailing slash)
  1492. destination_base_uri = S3Uri(dest)
  1493. if destination_base_uri.type != 's3':
  1494. raise ParameterError("Destination must be S3Uri. Got: %s" % destination_base_uri)
  1495. destination_base = destination_base_uri.uri()
  1496. return _child(destination_base, source_args)
  1497. def _parent(source_args):
  1498. # Now that we've done all the disk I/O to look at the local file system and
  1499. # calculate the md5 for each file, fork for each destination to upload to them separately
  1500. # and in parallel
  1501. child_pids = []
  1502. ret = EX_OK
  1503. for dest in destinations:
  1504. ## Normalize URI to convert s3://bkt to s3://bkt/ (trailing slash)
  1505. destination_base_uri = S3Uri(dest)
  1506. if destination_base_uri.type != 's3':
  1507. raise ParameterError("Destination must be S3Uri. Got: %s" % destination_base_uri)
  1508. destination_base = destination_base_uri.uri()
  1509. child_pid = os.fork()
  1510. if child_pid == 0:
  1511. os._exit(_child(destination_base, source_args))
  1512. else:
  1513. child_pids.append(child_pid)
  1514. while len(child_pids):
  1515. (pid, status) = os.wait()
  1516. child_pids.remove(pid)
  1517. if ret == EX_OK:
  1518. ret = os.WEXITSTATUS(status)
  1519. return ret
  1520. def _child(destination_base, source_args):
  1521. def _set_remote_uri(local_list, destination_base, single_file_local):
  1522. if len(local_list) > 0:
  1523. ## Populate 'remote_uri' only if we've got something to upload
  1524. if not destination_base.endswith("/"):
  1525. if not single_file_local:
  1526. raise ParameterError("Destination S3 URI must end with '/' (ie must refer to a directory on the remote side).")
  1527. local_list[local_list.keys()[0]]['remote_uri'] = destination_base
  1528. else:
  1529. for key in local_list:
  1530. local_list[key]['remote_uri'] = destination_base + key
  1531. def _upload(local_list, seq, total, total_size):
  1532. file_list = local_list.keys()
  1533. file_list.sort()
  1534. ret = EX_OK
  1535. for file in file_list:
  1536. seq += 1
  1537. item = local_list[file]
  1538. src = item['full_name']
  1539. try:
  1540. src_md5 = local_list.get_md5(file)
  1541. except IOError:
  1542. src_md5 = None
  1543. uri = S3Uri(item['remote_uri'])
  1544. seq_label = "[%d of %d]" % (seq, total)
  1545. extra_headers = copy(cfg.extra_headers)
  1546. try:
  1547. attr_header = _build_attr_header(local_list[file],
  1548. file, src_md5)
  1549. debug(u"attr_header: %s" % attr_header)
  1550. extra_headers.update(attr_header)
  1551. response = s3.object_put(src, uri, extra_headers, extra_label = seq_label)
  1552. except S3UploadError as exc:
  1553. error(u"Upload of '%s' failed too many times (Last reason: %s)" % (item['full_name'], exc))
  1554. if cfg.stop_on_error:
  1555. ret = EX_DATAERR
  1556. error(u"Exiting now because of --stop-on-error")
  1557. raise
  1558. ret = EX_PARTIAL
  1559. continue
  1560. except InvalidFileError as exc:
  1561. error(u"Upload of '%s' is not possible (Reason: %s)" % (item['full_name'], exc))
  1562. if cfg.stop_on_error:
  1563. ret = EX_OSFILE
  1564. error(u"Exiting now because of --stop-on-error")
  1565. raise
  1566. ret = EX_PARTIAL
  1567. continue
  1568. speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
  1569. if not cfg.progress_meter:
  1570. output(u"upload: '%s' -> '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s) %s" %
  1571. (item['full_name'], uri, response["size"], response["elapsed"],
  1572. speed_fmt[0], speed_fmt[1], seq_label))
  1573. total_size += response["size"]
  1574. uploaded_objects_list.append(uri.object())
  1575. return ret, seq, total_size
  1576. stats_info = StatsInfo()
  1577. local_list, single_file_local, src_exclude_list, local_total_size = fetch_local_list(args[:-1], is_src = True, recursive = True)
  1578. # - The source path is either like "/myPath/my_src_folder" and
  1579. # the user want to upload this single folder and optionally only delete
  1580. # things that have been removed inside this folder. For this case,
  1581. # we only have to look inside destination_base/my_src_folder and not at
  1582. # the root of destination_base.
  1583. # - Or like "/myPath/my_src_folder/" and the user want to have the sync
  1584. # with the content of this folder
  1585. # Special case, "." for current folder.
  1586. destbase_with_source_list = set()
  1587. for source_arg in source_args:
  1588. if not source_arg.endswith('/') and os.path.basename(source_arg) != '.' \
  1589. and not single_file_local:
  1590. destbase_with_source_list.add(os.path.join(destination_base,
  1591. os.path.basename(source_arg)))
  1592. else:
  1593. destbase_with_source_list.add(destination_base)
  1594. remote_list, dst_exclude_list, remote_total_size = fetch_remote_list(destbase_with_source_list, recursive = True, require_attribs = True)
  1595. local_count = len(local_list)
  1596. orig_local_count = local_count
  1597. remote_count = len(remote_list)
  1598. info(u"Found %d local files, %d remote files" % (local_count, remote_count))
  1599. if single_file_local and len(local_list) == 1 and len(remote_list) == 1:
  1600. ## Make remote_key same as local_key for comparison if we're dealing with only one file
  1601. remote_list_entry = remote_list[remote_list.keys()[0]]
  1602. # Flush remote_list, by the way
  1603. remote_list = FileDict()
  1604. remote_list[local_list.keys()[0]] = remote_list_entry
  1605. local_list, remote_list, update_list, copy_pairs = compare_filelists(local_list, remote_list, src_remote = False, dst_remote = True)
  1606. local_count = len(local_list)
  1607. update_count = len(update_list)
  1608. copy_count = len(copy_pairs)
  1609. remote_count = len(remote_list)
  1610. upload_count = local_count + update_count
  1611. info(u"Summary: %d local files to upload, %d files to remote copy, %d remote files to delete" % (upload_count, copy_count, remote_count))
  1612. _set_remote_uri(local_list, destination_base, single_file_local)
  1613. _set_remote_uri(update_list, destination_base, single_file_local)
  1614. if cfg.dry_run:
  1615. keys = filedicts_to_keys(src_exclude_list, dst_exclude_list)
  1616. for key in keys:
  1617. output(u"exclude: %s" % key)
  1618. for key in local_list:
  1619. output(u"upload: '%s' -> '%s'" % (local_list[key]['full_name'], local_list[key]['remote_uri']))
  1620. for key in update_list:
  1621. output(u"upload: '%s' -> '%s'" % (update_list[key]['full_name'], update_list[key]['remote_uri']))
  1622. for (src_obj, dst1, dst2, md5) in copy_pairs:
  1623. output(u"remote copy: '%s' -> '%s'" % (dst1, dst2))
  1624. if cfg.delete_removed:
  1625. for key in remote_list:
  1626. output(u"delete: '%s'" % remote_list[key]['object_uri_str'])
  1627. warning(u"Exiting now because of --dry-run")
  1628. return EX_OK
  1629. # if there are copy pairs, we can't do delete_before, on the chance
  1630. # we need one of the to-be-deleted files as a copy source.
  1631. if len(copy_pairs) > 0:
  1632. cfg.delete_after = True
  1633. if cfg.delete_removed and orig_local_count == 0 and len(remote_list) and not cfg.force:
  1634. warning(u"delete: cowardly refusing to delete because no source files were found. Use --force to override.")
  1635. cfg.delete_removed = False
  1636. if cfg.delete_removed and not cfg.delete_after and remote_list:
  1637. subcmd_batch_del(remote_list = remote_list)
  1638. size_transferred = 0
  1639. total_elapsed = 0.0
  1640. timestamp_start = time.time()
  1641. ret, n, size_transferred = _upload(local_list, 0, upload_count, size_transferred)
  1642. status, n, size_transferred = _upload(update_list, n, upload_count, size_transferred)
  1643. if ret == EX_OK:
  1644. ret = status
  1645. # uploaded_objects_list reference is passed so it can be filled with
  1646. # destination object of succcessful copies so that they can be
  1647. # invalidated by cf
  1648. n_copies, saved_bytes, failed_copy_files = remote_copy(
  1649. s3, copy_pairs, destination_base, uploaded_objects_list, True)
  1650. #upload file that could not be copied
  1651. debug("Process files that were not remotely copied")
  1652. failed_copy_count = len(failed_copy_files)
  1653. _set_remote_uri(failed_copy_files, destination_base, single_file_local)
  1654. status, n, size_transferred = _upload(failed_copy_files, n, upload_count + failed_copy_count, size_transferred)
  1655. if ret == EX_OK:
  1656. ret = status
  1657. if cfg.delete_removed and cfg.delete_after and remote_list:
  1658. subcmd_batch_del(remote_list = remote_list)
  1659. total_elapsed = max(1.0, time.time() - timestamp_start)
  1660. total_speed = total_elapsed and size_transferred / total_elapsed or 0.0
  1661. speed_fmt = formatSize(total_speed, human_readable = True, floating_point = True)
  1662. stats_info.files = orig_local_count
  1663. stats_info.size = local_total_size
  1664. stats_info.files_transferred = upload_count + failed_copy_count
  1665. stats_info.size_transferred = size_transferred
  1666. stats_info.files_copied = n_copies
  1667. stats_info.size_copied = saved_bytes
  1668. stats_info.files_deleted = remote_count
  1669. # Only print out the result if any work has been done or
  1670. # if the user asked for verbose output
  1671. outstr = "Done. Uploaded %d bytes in %0.1f seconds, %0.2f %sB/s." % (size_transferred, total_elapsed, speed_fmt[0], speed_fmt[1])
  1672. if cfg.stats:
  1673. outstr += stats_info.format_output()
  1674. output(outstr)
  1675. elif size_transferred + saved_bytes > 0:
  1676. output(outstr)
  1677. else:
  1678. info(outstr)
  1679. return ret
  1680. def _invalidate_on_cf(destination_base_uri):
  1681. cf = CloudFront(cfg)
  1682. default_index_file = None
  1683. if cfg.invalidate_default_index_on_cf or cfg.invalidate_default_index_root_on_cf:
  1684. info_response = s3.website_info(destination_base_uri, cfg.bucket_location)
  1685. if info_response:
  1686. default_index_file = info_response['index_document']
  1687. if len(default_index_file) < 1:
  1688. default_index_file = None
  1689. results = cf.InvalidateObjects(destination_base_uri, uploaded_objects_list, default_index_file, cfg.invalidate_default_index_on_cf, cfg.invalidate_default_index_root_on_cf)
  1690. for result in results:
  1691. if result['status'] == 201:
  1692. output(u"Created invalidation request for %d paths" % len(uploaded_objects_list))
  1693. output(u"Check progress with: s3cmd cfinvalinfo cf://%s/%s" % (result['dist_id'], result['request_id']))
  1694. # main execution
  1695. uploaded_objects_list = []
  1696. if cfg.encrypt:
  1697. error(u"S3cmd 'sync' doesn't yet support GPG encryption, sorry.")
  1698. error(u"Either use unconditional 's3cmd put --recursive'")
  1699. error(u"or disable encryption with --no-encrypt parameter.")
  1700. sys.exit(EX_USAGE)
  1701. for arg in args[:-1]:
  1702. if not os.path.exists(deunicodise(arg)):
  1703. raise ParameterError("Invalid source: '%s' is not an existing file or directory" % arg)
  1704. destinations = [args[-1]]
  1705. if cfg.additional_destinations:
  1706. destinations = destinations + cfg.additional_destinations
  1707. if 'fork' not in os.__all__ or len(destinations) < 2:
  1708. ret = _single_process(args[:-1])
  1709. destination_base_uri = S3Uri(destinations[-1])
  1710. if cfg.invalidate_on_cf:
  1711. if len(uploaded_objects_list) == 0:
  1712. info("Nothing to invalidate in CloudFront")
  1713. else:
  1714. _invalidate_on_cf(destination_base_uri)
  1715. else:
  1716. ret = _parent(args[:-1])
  1717. if cfg.invalidate_on_cf:
  1718. error(u"You cannot use both --cf-invalidate and --add-destination.")
  1719. return(EX_USAGE)
  1720. return ret
  1721. def cmd_sync(args):
  1722. cfg = Config()
  1723. if (len(args) < 2):
  1724. syntax_msg = ''
  1725. commands_list = get_commands_list()
  1726. for cmd in commands_list:
  1727. if cmd.get('cmd') == 'sync':
  1728. syntax_msg = cmd.get('param', '')
  1729. break
  1730. raise ParameterError("Too few parameters! Expected: %s" % syntax_msg)
  1731. if cfg.delay_updates:
  1732. warning(u"`delay-updates` is obsolete.")
  1733. for arg in args:
  1734. if arg == u'-':
  1735. raise ParameterError("Stdin or stdout ('-') can't be used for a source or a destination with the sync command.")
  1736. if S3Uri(args[0]).type == "file" and S3Uri(args[-1]).type == "s3":
  1737. return cmd_sync_local2remote(args)
  1738. if S3Uri(args[0]).type == "s3" and S3Uri(args[-1]).type == "file":
  1739. return cmd_sync_remote2local(args)
  1740. if S3Uri(args[0]).type == "s3" and S3Uri(args[-1]).type == "s3":
  1741. return cmd_sync_remote2remote(args)
  1742. raise ParameterError("Invalid source/destination: '%s'" % "' '".join(args))
  1743. def cmd_setacl(args):
  1744. cfg = Config()
  1745. s3 = S3(cfg)
  1746. set_to_acl = cfg.acl_public and "Public" or "Private"
  1747. if not cfg.recursive:
  1748. old_args = args
  1749. args = []
  1750. for arg in old_args:
  1751. uri = S3Uri(arg)
  1752. if not uri.has_object():
  1753. if cfg.acl_public != None:
  1754. info("Setting bucket-level ACL for %s to %s" % (uri.uri(), set_to_acl))
  1755. else:
  1756. info("Setting bucket-level ACL for %s" % (uri.uri()))
  1757. if not cfg.dry_run:
  1758. update_acl(s3, uri)
  1759. else:
  1760. args.append(arg)
  1761. remote_list, exclude_list, _ = fetch_remote_list(args)
  1762. remote_count = len(remote_list)
  1763. info(u"Summary: %d remote files to update" % remote_count)
  1764. if cfg.dry_run:
  1765. for key in exclude_list:
  1766. output(u"exclude: %s" % key)
  1767. for key in remote_list:
  1768. output(u"setacl: '%s'" % remote_list[key]['object_uri_str'])
  1769. warning(u"Exiting now because of --dry-run")
  1770. return EX_OK
  1771. seq = 0
  1772. for key in remote_list:
  1773. seq += 1
  1774. seq_label = "[%d of %d]" % (seq, remote_count)
  1775. uri = S3Uri(remote_list[key]['object_uri_str'])
  1776. update_acl(s3, uri, seq_label)
  1777. return EX_OK
  1778. def cmd_setpolicy(args):
  1779. cfg = Config()
  1780. s3 = S3(cfg)
  1781. uri = S3Uri(args[1])
  1782. policy_file = args[0]
  1783. with open(deunicodise(policy_file), 'r') as fp:
  1784. policy = fp.read()
  1785. if cfg.dry_run:
  1786. return EX_OK
  1787. response = s3.set_policy(uri, policy)
  1788. #if retsponse['status'] == 200:
  1789. debug(u"response - %s" % response['status'])
  1790. if response['status'] == 204:
  1791. output(u"%s: Policy updated" % uri)
  1792. return EX_OK
  1793. def cmd_delpolicy(args):
  1794. cfg = Config()
  1795. s3 = S3(cfg)
  1796. uri = S3Uri(args[0])
  1797. if cfg.dry_run: return EX_OK
  1798. response = s3.delete_policy(uri)
  1799. #if retsponse['status'] == 200:
  1800. debug(u"response - %s" % response['status'])
  1801. output(u"%s: Policy deleted" % uri)
  1802. return EX_OK
  1803. def cmd_setcors(args):
  1804. cfg = Config()
  1805. s3 = S3(cfg)
  1806. uri = S3Uri(args[1])
  1807. cors_file = args[0]
  1808. with open(deunicodise(cors_file), 'r') as fp:
  1809. cors = fp.read()
  1810. if cfg.dry_run:
  1811. return EX_OK
  1812. response = s3.set_cors(uri, cors)
  1813. #if retsponse['status'] == 200:
  1814. debug(u"response - %s" % response['status'])
  1815. if response['status'] == 204:
  1816. output(u"%s: CORS updated" % uri)
  1817. return EX_OK
  1818. def cmd_delcors(args):
  1819. cfg = Config()
  1820. s3 = S3(cfg)
  1821. uri = S3Uri(args[0])
  1822. if cfg.dry_run: return EX_OK
  1823. response = s3.delete_cors(uri)
  1824. #if retsponse['status'] == 200:
  1825. debug(u"response - %s" % response['status'])
  1826. output(u"%s: CORS deleted" % uri)
  1827. return EX_OK
  1828. def cmd_set_payer(args):
  1829. cfg = Config()
  1830. s3 = S3(cfg)
  1831. uri = S3Uri(args[0])
  1832. if cfg.dry_run: return EX_OK
  1833. response = s3.set_payer(uri)
  1834. if response['status'] == 200:
  1835. output(u"%s: Payer updated" % uri)
  1836. return EX_OK
  1837. else:
  1838. output(u"%s: Payer NOT updated" % uri)
  1839. return EX_CONFLICT
  1840. def cmd_setlifecycle(args):
  1841. cfg = Config()
  1842. s3 = S3(cfg)
  1843. uri = S3Uri(args[1])
  1844. lifecycle_policy_file = args[0]
  1845. with open(deunicodise(lifecycle_policy_file), 'r') as fp:
  1846. lifecycle_policy = fp.read()
  1847. if cfg.dry_run:
  1848. return EX_OK
  1849. response = s3.set_lifecycle_policy(uri, lifecycle_policy)
  1850. debug(u"response - %s" % response['status'])
  1851. if response['status'] == 200:
  1852. output(u"%s: Lifecycle Policy updated" % uri)
  1853. return EX_OK
  1854. def cmd_getlifecycle(args):
  1855. cfg = Config()
  1856. s3 = S3(cfg)
  1857. uri = S3Uri(args[0])
  1858. response = s3.get_lifecycle_policy(uri)
  1859. output(u"%s" % getPrettyFromXml(response['data']))
  1860. return EX_OK
  1861. def cmd_dellifecycle(args):
  1862. cfg = Config()
  1863. s3 = S3(cfg)
  1864. uri = S3Uri(args[0])
  1865. if cfg.dry_run: return EX_OK
  1866. response = s3.delete_lifecycle_policy(uri)
  1867. debug(u"response - %s" % response['status'])
  1868. output(u"%s: Lifecycle Policy deleted" % uri)
  1869. return EX_OK
  1870. def cmd_setnotification(args):
  1871. s3 = S3(Config())
  1872. uri = S3Uri(args[1])
  1873. notification_policy_file = args[0]
  1874. with open(deunicodise(notification_policy_file), 'r') as fp:
  1875. notification_policy = fp.read()
  1876. response = s3.set_notification_policy(uri, notification_policy)
  1877. debug(u"response - %s" % response['status'])
  1878. if response['status'] == 200:
  1879. output(u"%s: Notification Policy updated" % uri)
  1880. return EX_OK
  1881. def cmd_getnotification(args):
  1882. s3 = S3(Config())
  1883. uri = S3Uri(args[0])
  1884. response = s3.get_notification_policy(uri)
  1885. output(getPrettyFromXml(response['data']))
  1886. return EX_OK
  1887. def cmd_delnotification(args):
  1888. s3 = S3(Config())
  1889. uri = S3Uri(args[0])
  1890. response = s3.delete_notification_policy(uri)
  1891. debug(u"response - %s" % response['status'])
  1892. output(u"%s: Notification Policy deleted" % uri)
  1893. return EX_OK
  1894. def cmd_multipart(args):
  1895. cfg = Config()
  1896. s3 = S3(cfg)
  1897. uri = S3Uri(args[0])
  1898. #id = ''
  1899. #if(len(args) > 1): id = args[1]
  1900. upload_list = s3.get_multipart(uri)
  1901. output(u"%s" % uri)
  1902. debug(upload_list)
  1903. output(u"Initiated\tPath\tId")
  1904. for mpupload in upload_list:
  1905. try:
  1906. output(u"%s\t%s\t%s" % (
  1907. mpupload['Initiated'],
  1908. "s3://" + uri.bucket() + "/" + mpupload['Key'],
  1909. mpupload['UploadId']))
  1910. except KeyError:
  1911. pass
  1912. return EX_OK
  1913. def cmd_abort_multipart(args):
  1914. '''{"cmd":"abortmp", "label":"abort a multipart upload", "param":"s3://BUCKET Id", "func":cmd_abort_multipart, "argc":2},'''
  1915. cfg = Config()
  1916. s3 = S3(cfg)
  1917. uri = S3Uri(args[0])
  1918. id = args[1]
  1919. response = s3.abort_multipart(uri, id)
  1920. debug(u"response - %s" % response['status'])
  1921. output(u"%s" % uri)
  1922. return EX_OK
  1923. def cmd_list_multipart(args):
  1924. '''{"cmd":"abortmp", "label":"list a multipart upload", "param":"s3://BUCKET Id", "func":cmd_list_multipart, "argc":2},'''
  1925. cfg = Config()
  1926. s3 = S3(cfg)
  1927. uri = S3Uri(args[0])
  1928. id = args[1]
  1929. part_list = s3.list_multipart(uri, id)
  1930. output(u"LastModified\t\t\tPartNumber\tETag\tSize")
  1931. for mpupload in part_list:
  1932. try:
  1933. output(u"%s\t%s\t%s\t%s" % (mpupload['LastModified'],
  1934. mpupload['PartNumber'],
  1935. mpupload['ETag'],
  1936. mpupload['Size']))
  1937. except KeyError:
  1938. pass
  1939. return EX_OK
  1940. def cmd_accesslog(args):
  1941. cfg = Config()
  1942. s3 = S3(cfg)
  1943. bucket_uri = S3Uri(args.pop())
  1944. if bucket_uri.object():
  1945. raise ParameterError("Only bucket name is required for [accesslog] command")
  1946. if cfg.log_target_prefix == False:
  1947. accesslog, response = s3.set_accesslog(bucket_uri, enable = False)
  1948. elif cfg.log_target_prefix:
  1949. log_target_prefix_uri = S3Uri(cfg.log_target_prefix)
  1950. if log_target_prefix_uri.type != "s3":
  1951. raise ParameterError("--log-target-prefix must be a S3 URI")
  1952. accesslog, response = s3.set_accesslog(bucket_uri, enable = True, log_target_prefix_uri = log_target_prefix_uri, acl_public = cfg.acl_public)
  1953. else: # cfg.log_target_prefix == None
  1954. accesslog = s3.get_accesslog(bucket_uri)
  1955. output(u"Access logging for: %s" % bucket_uri.uri())
  1956. output(u" Logging Enabled: %s" % accesslog.isLoggingEnabled())
  1957. if accesslog.isLoggingEnabled():
  1958. output(u" Target prefix: %s" % accesslog.targetPrefix().uri())
  1959. #output(u" Public Access: %s" % accesslog.isAclPublic())
  1960. return EX_OK
  1961. def cmd_sign(args):
  1962. string_to_sign = args.pop()
  1963. debug(u"string-to-sign: %r" % string_to_sign)
  1964. signature = Crypto.sign_string_v2(encode_to_s3(string_to_sign))
  1965. output(u"Signature: %s" % decode_from_s3(signature))
  1966. return EX_OK
  1967. def cmd_signurl(args):
  1968. expiry = args.pop()
  1969. url_to_sign = S3Uri(args.pop())
  1970. if url_to_sign.type != 's3':
  1971. raise ParameterError("Must be S3Uri. Got: %s" % url_to_sign)
  1972. debug("url to sign: %r" % url_to_sign)
  1973. signed_url = Crypto.sign_url_v2(url_to_sign, expiry)
  1974. output(signed_url)
  1975. return EX_OK
  1976. def cmd_fixbucket(args):
  1977. def _unescape(text):
  1978. ##
  1979. # Removes HTML or XML character references and entities from a text string.
  1980. #
  1981. # @param text The HTML (or XML) source text.
  1982. # @return The plain text, as a Unicode string, if necessary.
  1983. #
  1984. # From: http://effbot.org/zone/re-sub.htm#unescape-html
  1985. def _unescape_fixup(m):
  1986. text = m.group(0)
  1987. if not 'apos' in htmlentitydefs.name2codepoint:
  1988. htmlentitydefs.name2codepoint['apos'] = ord("'")
  1989. if text[:2] == "&#":
  1990. # character reference
  1991. try:
  1992. if text[:3] == "&#x":
  1993. return unichr(int(text[3:-1], 16))
  1994. else:
  1995. return unichr(int(text[2:-1]))
  1996. except ValueError:
  1997. pass
  1998. else:
  1999. # named entity
  2000. try:
  2001. text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
  2002. except KeyError:
  2003. pass
  2004. return text # leave as is
  2005. text = text.encode('ascii', 'xmlcharrefreplace')
  2006. return re.sub(r"&#?\w+;", _unescape_fixup, text)
  2007. cfg = Config()
  2008. cfg.urlencoding_mode = "fixbucket"
  2009. s3 = S3(cfg)
  2010. count = 0
  2011. for arg in args:
  2012. culprit = S3Uri(arg)
  2013. if culprit.type != "s3":
  2014. raise ParameterError("Expecting S3Uri instead of: %s" % arg)
  2015. response = s3.bucket_list_noparse(culprit.bucket(), culprit.object(), recursive = True)
  2016. r_xent = re.compile(r"&#x[\da-fA-F]+;")
  2017. data = decode_from_s3(response['data'])
  2018. keys = re.findall("<Key>(.*?)</Key>", data, re.MULTILINE | re.UNICODE)
  2019. debug("Keys: %r" % keys)
  2020. for key in keys:
  2021. if r_xent.search(key):
  2022. info("Fixing: %s" % key)
  2023. debug("Step 1: Transforming %s" % key)
  2024. key_bin = _unescape(key)
  2025. debug("Step 2: ... to %s" % key_bin)
  2026. key_new = replace_nonprintables(key_bin)
  2027. debug("Step 3: ... then to %s" % key_new)
  2028. src = S3Uri("s3://%s/%s" % (culprit.bucket(), key_bin))
  2029. dst = S3Uri("s3://%s/%s" % (culprit.bucket(), key_new))
  2030. if cfg.dry_run:
  2031. output(u"[--dry-run] File %r would be renamed to %s" % (key_bin, key_new))
  2032. continue
  2033. try:
  2034. resp_move = s3.object_move(src, dst)
  2035. if resp_move['status'] == 200:
  2036. output(u"File '%r' renamed to '%s'" % (key_bin, key_new))
  2037. count += 1
  2038. else:
  2039. error(u"Something went wrong for: %r" % key)
  2040. error(u"Please report the problem to s3tools-bugs@lists.sourceforge.net")
  2041. except S3Error:
  2042. error(u"Something went wrong for: %r" % key)
  2043. error(u"Please report the problem to s3tools-bugs@lists.sourceforge.net")
  2044. if count > 0:
  2045. warning(u"Fixed %d files' names. Their ACL were reset to Private." % count)
  2046. warning(u"Use 's3cmd setacl --acl-public s3://...' to make")
  2047. warning(u"them publicly readable if required.")
  2048. return EX_OK
  2049. def resolve_list(lst, args):
  2050. retval = []
  2051. for item in lst:
  2052. retval.append(item % args)
  2053. return retval
  2054. def gpg_command(command, passphrase = ""):
  2055. debug(u"GPG command: " + " ".join(command))
  2056. command = [deunicodise(cmd_entry) for cmd_entry in command]
  2057. p = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT,
  2058. close_fds = True)
  2059. p_stdout, p_stderr = p.communicate(deunicodise(passphrase + "\n"))
  2060. debug(u"GPG output:")
  2061. for line in unicodise(p_stdout).split("\n"):
  2062. debug(u"GPG: " + line)
  2063. p_exitcode = p.wait()
  2064. return p_exitcode
  2065. def gpg_encrypt(filename):
  2066. cfg = Config()
  2067. tmp_filename = Utils.mktmpfile()
  2068. args = {
  2069. "gpg_command" : cfg.gpg_command,
  2070. "passphrase_fd" : "0",
  2071. "input_file" : filename,
  2072. "output_file" : tmp_filename,
  2073. }
  2074. info(u"Encrypting file %s to %s..." % (filename, tmp_filename))
  2075. command = resolve_list(cfg.gpg_encrypt.split(" "), args)
  2076. code = gpg_command(command, cfg.gpg_passphrase)
  2077. return (code, tmp_filename, "gpg")
  2078. def gpg_decrypt(filename, gpgenc_header = "", in_place = True):
  2079. cfg = Config()
  2080. tmp_filename = Utils.mktmpfile(filename)
  2081. args = {
  2082. "gpg_command" : cfg.gpg_command,
  2083. "passphrase_fd" : "0",
  2084. "input_file" : filename,
  2085. "output_file" : tmp_filename,
  2086. }
  2087. info(u"Decrypting file %s to %s..." % (filename, tmp_filename))
  2088. command = resolve_list(cfg.gpg_decrypt.split(" "), args)
  2089. code = gpg_command(command, cfg.gpg_passphrase)
  2090. if code == 0 and in_place:
  2091. debug(u"Renaming %s to %s" % (tmp_filename, filename))
  2092. os.unlink(deunicodise(filename))
  2093. os.rename(deunicodise(tmp_filename), deunicodise(filename))
  2094. tmp_filename = filename
  2095. return (code, tmp_filename)
  2096. def run_configure(config_file, args):
  2097. cfg = Config()
  2098. options = [
  2099. ("access_key", "Access Key", "Access key and Secret key are your identifiers for Amazon S3. Leave them empty for using the env variables."),
  2100. ("secret_key", "Secret Key"),
  2101. ("bucket_location", "Default Region"),
  2102. ("host_base", "S3 Endpoint", "Use \"s3.amazonaws.com\" for S3 Endpoint and not modify it to the target Amazon S3."),
  2103. ("host_bucket", "DNS-style bucket+hostname:port template for accessing a bucket", "Use \"%(bucket)s.s3.amazonaws.com\" to the target Amazon S3. \"%(bucket)s\" and \"%(location)s\" vars can be used\nif the target S3 system supports dns based buckets."),
  2104. ("gpg_passphrase", "Hash password", "Hash password is used to protect your files from reading\nby unauthorized persons while in transfer to S3"),
  2105. ("gpg_command", "Path to GPG program"),
  2106. ("use_https", "Use HTTPS protocol", "When using secure HTTPS protocol all communication with Amazon S3\nservers is protected from 3rd party eavesdropping. This method is\nslower than plain HTTP, and can only be proxied with Python 2.7 or newer"),
  2107. ("proxy_host", "HTTP Proxy server name", "On some networks all internet access must go through a HTTP proxy.\nTry setting it here if you can't connect to S3 directly"),
  2108. ("proxy_port", "HTTP Proxy server port"),
  2109. ]
  2110. ## Option-specfic defaults
  2111. if getattr(cfg, "gpg_command") == "":
  2112. setattr(cfg, "gpg_command", which("gpg"))
  2113. if getattr(cfg, "proxy_host") == "" and os.getenv("http_proxy"):
  2114. autodetected_encoding = locale.getpreferredencoding() or "UTF-8"
  2115. re_match=re.match(r"(http://)?([^:]+):(\d+)",
  2116. unicodise_s(os.getenv("http_proxy"), autodetected_encoding))
  2117. if re_match:
  2118. setattr(cfg, "proxy_host", re_match.groups()[1])
  2119. setattr(cfg, "proxy_port", re_match.groups()[2])
  2120. try:
  2121. # Support for python3
  2122. # raw_input only exists in py2 and was renamed to input in py3
  2123. global input
  2124. input = raw_input
  2125. except NameError:
  2126. pass
  2127. try:
  2128. while True:
  2129. output(u"\nEnter new values or accept defaults in brackets with Enter.")
  2130. output(u"Refer to user manual for detailed description of all options.")
  2131. for option in options:
  2132. prompt = option[1]
  2133. ## Option-specific handling
  2134. if option[0] == 'proxy_host' and getattr(cfg, 'use_https') == True and sys.hexversion < 0x02070000:
  2135. setattr(cfg, option[0], "")
  2136. continue
  2137. if option[0] == 'proxy_port' and getattr(cfg, 'proxy_host') == "":
  2138. setattr(cfg, option[0], 0)
  2139. continue
  2140. try:
  2141. val = getattr(cfg, option[0])
  2142. if type(val) is bool:
  2143. val = val and "Yes" or "No"
  2144. if val not in (None, ""):
  2145. prompt += " [%s]" % val
  2146. except AttributeError:
  2147. pass
  2148. if len(option) >= 3:
  2149. output(u"\n%s" % option[2])
  2150. val = unicodise_s(input(prompt + ": "))
  2151. if val != "":
  2152. if type(getattr(cfg, option[0])) is bool:
  2153. # Turn 'Yes' into True, everything else into False
  2154. val = val.lower().startswith('y')
  2155. setattr(cfg, option[0], val)
  2156. output(u"\nNew settings:")
  2157. for option in options:
  2158. output(u" %s: %s" % (option[1], getattr(cfg, option[0])))
  2159. val = input("\nTest access with supplied credentials? [Y/n] ")
  2160. if val.lower().startswith("y") or val == "":
  2161. try:
  2162. # Default, we try to list 'all' buckets which requires
  2163. # ListAllMyBuckets permission
  2164. if len(args) == 0:
  2165. output(u"Please wait, attempting to list all buckets...")
  2166. S3(Config()).bucket_list("", "")
  2167. else:
  2168. # If user specified a bucket name directly, we check it and only it.
  2169. # Thus, access check can succeed even if user only has access to
  2170. # to a single bucket and not ListAllMyBuckets permission.
  2171. output(u"Please wait, attempting to list bucket: " + args[0])
  2172. uri = S3Uri(args[0])
  2173. if uri.type == "s3" and uri.has_bucket():
  2174. S3(Config()).bucket_list(uri.bucket(), "")
  2175. else:
  2176. raise Exception(u"Invalid bucket uri: " + args[0])
  2177. output(u"Success. Your access key and secret key worked fine :-)")
  2178. output(u"\nNow verifying that encryption works...")
  2179. if not getattr(cfg, "gpg_command") or not getattr(cfg, "gpg_passphrase"):
  2180. output(u"Not configured. Never mind.")
  2181. else:
  2182. if not getattr(cfg, "gpg_command"):
  2183. raise Exception("Path to GPG program not set")
  2184. if not os.path.isfile(deunicodise(getattr(cfg, "gpg_command"))):
  2185. raise Exception("GPG program not found")
  2186. filename = Utils.mktmpfile()
  2187. with open(deunicodise(filename), "w") as fp:
  2188. fp.write(os.sys.copyright)
  2189. ret_enc = gpg_encrypt(filename)
  2190. ret_dec = gpg_decrypt(ret_enc[1], ret_enc[2], False)
  2191. hash = [
  2192. Utils.hash_file_md5(filename),
  2193. Utils.hash_file_md5(ret_enc[1]),
  2194. Utils.hash_file_md5(ret_dec[1]),
  2195. ]
  2196. os.unlink(deunicodise(filename))
  2197. os.unlink(deunicodise(ret_enc[1]))
  2198. os.unlink(deunicodise(ret_dec[1]))
  2199. if hash[0] == hash[2] and hash[0] != hash[1]:
  2200. output(u"Success. Hash and decryption worked fine :-)")
  2201. else:
  2202. raise Exception("Hash verification error.")
  2203. except S3Error as e:
  2204. error(u"Test failed: %s" % (e))
  2205. if e.code == "AccessDenied":
  2206. error(u"Are you sure your keys have s3:ListAllMyBuckets permissions?")
  2207. val = input("\nRetry configuration? [Y/n] ")
  2208. if val.lower().startswith("y") or val == "":
  2209. continue
  2210. except Exception as e:
  2211. error(u"Test failed: %s" % (e))
  2212. val = input("\nRetry configuration? [Y/n] ")
  2213. if val.lower().startswith("y") or val == "":
  2214. continue
  2215. val = input("\nSave settings? [y/N] ")
  2216. if val.lower().startswith("y"):
  2217. break
  2218. val = input("Retry configuration? [Y/n] ")
  2219. if val.lower().startswith("n"):
  2220. raise EOFError()
  2221. ## Overwrite existing config file, make it user-readable only
  2222. old_mask = os.umask(0o077)
  2223. try:
  2224. os.remove(deunicodise(config_file))
  2225. except OSError as e:
  2226. if e.errno != errno.ENOENT:
  2227. raise
  2228. try:
  2229. with io.open(deunicodise(config_file), "w", encoding=cfg.encoding) as fp:
  2230. cfg.dump_config(fp)
  2231. finally:
  2232. os.umask(old_mask)
  2233. output(u"Configuration saved to '%s'" % config_file)
  2234. except (EOFError, KeyboardInterrupt):
  2235. output(u"\nConfiguration aborted. Changes were NOT saved.")
  2236. return
  2237. except IOError as e:
  2238. error(u"Writing config file failed: %s: %s" % (config_file, e.strerror))
  2239. sys.exit(EX_IOERR)
  2240. def process_patterns_from_file(fname, patterns_list):
  2241. try:
  2242. with open(deunicodise(fname), "rt") as fn:
  2243. for pattern in fn:
  2244. pattern = unicodise(pattern).strip()
  2245. if re.match("^#", pattern) or re.match(r"^\s*$", pattern):
  2246. continue
  2247. debug(u"%s: adding rule: %s" % (fname, pattern))
  2248. patterns_list.append(pattern)
  2249. except IOError as e:
  2250. error(e)
  2251. sys.exit(EX_IOERR)
  2252. return patterns_list
  2253. def process_patterns(patterns_list, patterns_from, is_glob, option_txt = ""):
  2254. r"""
  2255. process_patterns(patterns, patterns_from, is_glob, option_txt = "")
  2256. Process --exclude / --include GLOB and REGEXP patterns.
  2257. 'option_txt' is 'exclude' / 'include' / 'rexclude' / 'rinclude'
  2258. Returns: patterns_compiled, patterns_text
  2259. Note: process_patterns_from_file will ignore lines starting with # as these
  2260. are comments. To target escape the initial #, to use it in a file name, one
  2261. can use: "[#]" (for exclude) or "\#" (for rexclude).
  2262. """
  2263. patterns_compiled = []
  2264. patterns_textual = {}
  2265. if patterns_list is None:
  2266. patterns_list = []
  2267. if patterns_from:
  2268. ## Append patterns from glob_from
  2269. for fname in patterns_from:
  2270. debug(u"processing --%s-from %s" % (option_txt, fname))
  2271. patterns_list = process_patterns_from_file(fname, patterns_list)
  2272. for pattern in patterns_list:
  2273. debug(u"processing %s rule: %s" % (option_txt, patterns_list))
  2274. if is_glob:
  2275. pattern = glob.fnmatch.translate(pattern)
  2276. r = re.compile(pattern)
  2277. patterns_compiled.append(r)
  2278. patterns_textual[r] = pattern
  2279. return patterns_compiled, patterns_textual
  2280. def get_commands_list():
  2281. return [
  2282. {"cmd":"mb", "label":"Make bucket", "param":"s3://BUCKET", "func":cmd_bucket_create, "argc":1},
  2283. {"cmd":"rb", "label":"Remove bucket", "param":"s3://BUCKET", "func":cmd_bucket_delete, "argc":1},
  2284. {"cmd":"ls", "label":"List objects or buckets", "param":"[s3://BUCKET[/PREFIX]]", "func":cmd_ls, "argc":0},
  2285. {"cmd":"la", "label":"List all object in all buckets", "param":"", "func":cmd_all_buckets_list_all_content, "argc":0},
  2286. {"cmd":"put", "label":"Put file into bucket", "param":"FILE [FILE...] s3://BUCKET[/PREFIX]", "func":cmd_object_put, "argc":2},
  2287. {"cmd":"get", "label":"Get file from bucket", "param":"s3://BUCKET/OBJECT LOCAL_FILE", "func":cmd_object_get, "argc":1},
  2288. {"cmd":"del", "label":"Delete file from bucket", "param":"s3://BUCKET/OBJECT", "func":cmd_object_del, "argc":1},
  2289. {"cmd":"rm", "label":"Delete file from bucket (alias for del)", "param":"s3://BUCKET/OBJECT", "func":cmd_object_del, "argc":1},
  2290. #{"cmd":"mkdir", "label":"Make a virtual S3 directory", "param":"s3://BUCKET/path/to/dir", "func":cmd_mkdir, "argc":1},
  2291. {"cmd":"restore", "label":"Restore file from Glacier storage", "param":"s3://BUCKET/OBJECT", "func":cmd_object_restore, "argc":1},
  2292. {"cmd":"sync", "label":"Synchronize a directory tree to S3 (checks files freshness using size and md5 checksum, unless overridden by options, see below)", "param":"LOCAL_DIR s3://BUCKET[/PREFIX] or s3://BUCKET[/PREFIX] LOCAL_DIR or s3://BUCKET[/PREFIX] s3://BUCKET[/PREFIX]", "func":cmd_sync, "argc":2},
  2293. {"cmd":"du", "label":"Disk usage by buckets", "param":"[s3://BUCKET[/PREFIX]]", "func":cmd_du, "argc":0},
  2294. {"cmd":"info", "label":"Get various information about Buckets or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_info, "argc":1},
  2295. {"cmd":"cp", "label":"Copy object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_cp, "argc":2},
  2296. {"cmd":"modify", "label":"Modify object metadata", "param":"s3://BUCKET1/OBJECT", "func":cmd_modify, "argc":1},
  2297. {"cmd":"mv", "label":"Move object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_mv, "argc":2},
  2298. {"cmd":"setacl", "label":"Modify Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_setacl, "argc":1},
  2299. {"cmd":"setpolicy", "label":"Modify Bucket Policy", "param":"FILE s3://BUCKET", "func":cmd_setpolicy, "argc":2},
  2300. {"cmd":"delpolicy", "label":"Delete Bucket Policy", "param":"s3://BUCKET", "func":cmd_delpolicy, "argc":1},
  2301. {"cmd":"setcors", "label":"Modify Bucket CORS", "param":"FILE s3://BUCKET", "func":cmd_setcors, "argc":2},
  2302. {"cmd":"delcors", "label":"Delete Bucket CORS", "param":"s3://BUCKET", "func":cmd_delcors, "argc":1},
  2303. {"cmd":"payer", "label":"Modify Bucket Requester Pays policy", "param":"s3://BUCKET", "func":cmd_set_payer, "argc":1},
  2304. {"cmd":"multipart", "label":"Show multipart uploads", "param":"s3://BUCKET [Id]", "func":cmd_multipart, "argc":1},
  2305. {"cmd":"abortmp", "label":"Abort a multipart upload", "param":"s3://BUCKET/OBJECT Id", "func":cmd_abort_multipart, "argc":2},
  2306. {"cmd":"listmp", "label":"List parts of a multipart upload", "param":"s3://BUCKET/OBJECT Id", "func":cmd_list_multipart, "argc":2},
  2307. {"cmd":"accesslog", "label":"Enable/disable bucket access logging", "param":"s3://BUCKET", "func":cmd_accesslog, "argc":1},
  2308. {"cmd":"sign", "label":"Sign arbitrary string using the secret key", "param":"STRING-TO-SIGN", "func":cmd_sign, "argc":1},
  2309. {"cmd":"signurl", "label":"Sign an S3 URL to provide limited public access with expiry", "param":"s3://BUCKET/OBJECT <expiry_epoch|+expiry_offset>", "func":cmd_signurl, "argc":2},
  2310. {"cmd":"fixbucket", "label":"Fix invalid file names in a bucket", "param":"s3://BUCKET[/PREFIX]", "func":cmd_fixbucket, "argc":1},
  2311. ## Website commands
  2312. {"cmd":"ws-create", "label":"Create Website from bucket", "param":"s3://BUCKET", "func":cmd_website_create, "argc":1},
  2313. {"cmd":"ws-delete", "label":"Delete Website", "param":"s3://BUCKET", "func":cmd_website_delete, "argc":1},
  2314. {"cmd":"ws-info", "label":"Info about Website", "param":"s3://BUCKET", "func":cmd_website_info, "argc":1},
  2315. ## Lifecycle commands
  2316. {"cmd":"expire", "label":"Set or delete expiration rule for the bucket", "param":"s3://BUCKET", "func":cmd_expiration_set, "argc":1},
  2317. {"cmd":"setlifecycle", "label":"Upload a lifecycle policy for the bucket", "param":"FILE s3://BUCKET", "func":cmd_setlifecycle, "argc":2},
  2318. {"cmd":"getlifecycle", "label":"Get a lifecycle policy for the bucket", "param":"s3://BUCKET", "func":cmd_getlifecycle, "argc":1},
  2319. {"cmd":"dellifecycle", "label":"Remove a lifecycle policy for the bucket", "param":"s3://BUCKET", "func":cmd_dellifecycle, "argc":1},
  2320. ## Notification commands
  2321. {"cmd":"setnotification", "label":"Upload a notification policy for the bucket", "param":"FILE s3://BUCKET", "func":cmd_setnotification, "argc":2},
  2322. {"cmd":"getnotification", "label":"Get a notification policy for the bucket", "param":"s3://BUCKET", "func":cmd_getnotification, "argc":1},
  2323. {"cmd":"delnotification", "label":"Remove a notification policy for the bucket", "param":"s3://BUCKET", "func":cmd_delnotification, "argc":1},
  2324. ## CloudFront commands
  2325. {"cmd":"cflist", "label":"List CloudFront distribution points", "param":"", "func":CfCmd.info, "argc":0},
  2326. {"cmd":"cfinfo", "label":"Display CloudFront distribution point parameters", "param":"[cf://DIST_ID]", "func":CfCmd.info, "argc":0},
  2327. {"cmd":"cfcreate", "label":"Create CloudFront distribution point", "param":"s3://BUCKET", "func":CfCmd.create, "argc":1},
  2328. {"cmd":"cfdelete", "label":"Delete CloudFront distribution point", "param":"cf://DIST_ID", "func":CfCmd.delete, "argc":1},
  2329. {"cmd":"cfmodify", "label":"Change CloudFront distribution point parameters", "param":"cf://DIST_ID", "func":CfCmd.modify, "argc":1},
  2330. {"cmd":"cfinval", "label":"Invalidate CloudFront objects", "param":"s3://BUCKET/OBJECT [s3://BUCKET/OBJECT ...]", "func":CfCmd.invalidate, "argc":1},
  2331. {"cmd":"cfinvalinfo", "label":"Display CloudFront invalidation request(s) status", "param":"cf://DIST_ID[/INVAL_ID]", "func":CfCmd.invalinfo, "argc":1},
  2332. ]
  2333. def format_commands(progname, commands_list):
  2334. help = "Commands:\n"
  2335. for cmd in commands_list:
  2336. help += " %s\n %s %s %s\n" % (cmd["label"], progname, cmd["cmd"], cmd["param"])
  2337. return help
  2338. def update_acl(s3, uri, seq_label=""):
  2339. cfg = Config()
  2340. something_changed = False
  2341. acl = s3.get_acl(uri)
  2342. debug(u"acl: %s - %r" % (uri, acl.grantees))
  2343. if cfg.acl_public == True:
  2344. if acl.isAnonRead():
  2345. info(u"%s: already Public, skipping %s" % (uri, seq_label))
  2346. else:
  2347. acl.grantAnonRead()
  2348. something_changed = True
  2349. elif cfg.acl_public == False: # we explicitely check for False, because it could be None
  2350. if not acl.isAnonRead() and not acl.isAnonWrite():
  2351. info(u"%s: already Private, skipping %s" % (uri, seq_label))
  2352. else:
  2353. acl.revokeAnonRead()
  2354. acl.revokeAnonWrite()
  2355. something_changed = True
  2356. # update acl with arguments
  2357. # grant first and revoke later, because revoke has priority
  2358. if cfg.acl_grants:
  2359. something_changed = True
  2360. for grant in cfg.acl_grants:
  2361. acl.grant(**grant)
  2362. if cfg.acl_revokes:
  2363. something_changed = True
  2364. for revoke in cfg.acl_revokes:
  2365. acl.revoke(**revoke)
  2366. if not something_changed:
  2367. return
  2368. retsponse = s3.set_acl(uri, acl)
  2369. if retsponse['status'] == 200:
  2370. if cfg.acl_public in (True, False):
  2371. set_to_acl = cfg.acl_public and "Public" or "Private"
  2372. output(u"%s: ACL set to %s %s" % (uri, set_to_acl, seq_label))
  2373. else:
  2374. output(u"%s: ACL updated" % uri)
  2375. class OptionMimeType(Option):
  2376. def check_mimetype(self, opt, value):
  2377. if re.compile(r"^[a-z0-9]+/[a-z0-9+\.-]+(;.*)?$", re.IGNORECASE).match(value):
  2378. return value
  2379. raise OptionValueError("option %s: invalid MIME-Type format: %r" % (opt, value))
  2380. class OptionS3ACL(Option):
  2381. def check_s3acl(self, opt, value):
  2382. permissions = ('read', 'write', 'read_acp', 'write_acp', 'full_control', 'all')
  2383. try:
  2384. permission, grantee = re.compile(r"^(\w+):(.+)$", re.IGNORECASE).match(value).groups()
  2385. if not permission or not grantee:
  2386. raise OptionValueError("option %s: invalid S3 ACL format: %r" % (opt, value))
  2387. if permission in permissions:
  2388. return { 'name' : grantee, 'permission' : permission.upper() }
  2389. else:
  2390. raise OptionValueError("option %s: invalid S3 ACL permission: %s (valid values: %s)" %
  2391. (opt, permission, ", ".join(permissions)))
  2392. except OptionValueError:
  2393. raise
  2394. except Exception:
  2395. raise OptionValueError("option %s: invalid S3 ACL format: %r" % (opt, value))
  2396. class OptionAll(OptionMimeType, OptionS3ACL):
  2397. TYPE_CHECKER = copy(Option.TYPE_CHECKER)
  2398. TYPE_CHECKER["mimetype"] = OptionMimeType.check_mimetype
  2399. TYPE_CHECKER["s3acl"] = OptionS3ACL.check_s3acl
  2400. TYPES = Option.TYPES + ("mimetype", "s3acl")
  2401. class MyHelpFormatter(IndentedHelpFormatter):
  2402. def format_epilog(self, epilog):
  2403. if epilog:
  2404. return "\n" + epilog + "\n"
  2405. else:
  2406. return ""
  2407. def main():
  2408. cfg = Config()
  2409. commands_list = get_commands_list()
  2410. commands = {}
  2411. ## Populate "commands" from "commands_list"
  2412. for cmd in commands_list:
  2413. if 'cmd' in cmd:
  2414. commands[cmd['cmd']] = cmd
  2415. optparser = OptionParser(option_class=OptionAll, formatter=MyHelpFormatter())
  2416. #optparser.disable_interspersed_args()
  2417. autodetected_encoding = locale.getpreferredencoding() or "UTF-8"
  2418. config_file = None
  2419. if os.getenv("S3CMD_CONFIG"):
  2420. config_file = unicodise_s(os.getenv("S3CMD_CONFIG"),
  2421. autodetected_encoding)
  2422. elif os.name == "nt" and os.getenv("USERPROFILE"):
  2423. config_file = os.path.join(
  2424. unicodise_s(os.getenv("USERPROFILE"), autodetected_encoding),
  2425. os.getenv("APPDATA")
  2426. and unicodise_s(os.getenv("APPDATA"), autodetected_encoding)
  2427. or 'Application Data',
  2428. "s3cmd.ini")
  2429. else:
  2430. from os.path import expanduser
  2431. config_file = os.path.join(expanduser("~"), ".s3cfg")
  2432. optparser.set_defaults(config = config_file)
  2433. optparser.add_option( "--configure", dest="run_configure", action="store_true", help="Invoke interactive (re)configuration tool. Optionally use as '--configure s3://some-bucket' to test access to a specific bucket instead of attempting to list them all.")
  2434. optparser.add_option("-c", "--config", dest="config", metavar="FILE", help="Config file name. Defaults to $HOME/.s3cfg")
  2435. optparser.add_option( "--dump-config", dest="dump_config", action="store_true", help="Dump current configuration after parsing config files and command line options and exit.")
  2436. optparser.add_option( "--access_key", dest="access_key", help="AWS Access Key")
  2437. optparser.add_option( "--secret_key", dest="secret_key", help="AWS Secret Key")
  2438. optparser.add_option( "--access_token", dest="access_token", help="AWS Access Token")
  2439. optparser.add_option("-n", "--dry-run", dest="dry_run", action="store_true", help="Only show what should be uploaded or downloaded but don't actually do it. May still perform S3 requests to get bucket listings and other information though (only for file transfer commands)")
  2440. optparser.add_option("-s", "--ssl", dest="use_https", action="store_true", help="Use HTTPS connection when communicating with S3. (default)")
  2441. optparser.add_option( "--no-ssl", dest="use_https", action="store_false", help="Don't use HTTPS.")
  2442. optparser.add_option("-e", "--encrypt", dest="encrypt", action="store_true", help="Encrypt files before uploading to S3.")
  2443. optparser.add_option( "--no-encrypt", dest="encrypt", action="store_false", help="Don't encrypt files.")
  2444. optparser.add_option("-f", "--force", dest="force", action="store_true", help="Force overwrite and other dangerous operations.")
  2445. optparser.add_option( "--continue", dest="get_continue", action="store_true", help="Continue getting a partially downloaded file (only for [get] command).")
  2446. optparser.add_option( "--continue-put", dest="put_continue", action="store_true", help="Continue uploading partially uploaded files or multipart upload parts. Restarts parts/files that don't have matching size and md5. Skips files/parts that do. Note: md5sum checks are not always sufficient to check (part) file equality. Enable this at your own risk.")
  2447. optparser.add_option( "--upload-id", dest="upload_id", help="UploadId for Multipart Upload, in case you want continue an existing upload (equivalent to --continue-put) and there are multiple partial uploads. Use s3cmd multipart [URI] to see what UploadIds are associated with the given URI.")
  2448. optparser.add_option( "--skip-existing", dest="skip_existing", action="store_true", help="Skip over files that exist at the destination (only for [get] and [sync] commands).")
  2449. optparser.add_option("-r", "--recursive", dest="recursive", action="store_true", help="Recursive upload, download or removal.")
  2450. optparser.add_option( "--check-md5", dest="check_md5", action="store_true", help="Check MD5 sums when comparing files for [sync]. (default)")
  2451. optparser.add_option( "--no-check-md5", dest="check_md5", action="store_false", help="Do not check MD5 sums when comparing files for [sync]. Only size will be compared. May significantly speed up transfer but may also miss some changed files.")
  2452. optparser.add_option("-P", "--acl-public", dest="acl_public", action="store_true", help="Store objects with ACL allowing read for anyone.")
  2453. optparser.add_option( "--acl-private", dest="acl_public", action="store_false", help="Store objects with default ACL allowing access for you only.")
  2454. optparser.add_option( "--acl-grant", dest="acl_grants", type="s3acl", action="append", metavar="PERMISSION:EMAIL or USER_CANONICAL_ID", help="Grant stated permission to a given amazon user. Permission is one of: read, write, read_acp, write_acp, full_control, all")
  2455. optparser.add_option( "--acl-revoke", dest="acl_revokes", type="s3acl", action="append", metavar="PERMISSION:USER_CANONICAL_ID", help="Revoke stated permission for a given amazon user. Permission is one of: read, write, read_acp, write_acp, full_control, all")
  2456. optparser.add_option("-D", "--restore-days", dest="restore_days", action="store", help="Number of days to keep restored file available (only for 'restore' command). Default is 1 day.", metavar="NUM")
  2457. optparser.add_option( "--restore-priority", dest="restore_priority", action="store", choices=['standard', 'expedited', 'bulk'], help="Priority for restoring files from S3 Glacier (only for 'restore' command). Choices available: bulk, standard, expedited")
  2458. optparser.add_option( "--delete-removed", dest="delete_removed", action="store_true", help="Delete destination objects with no corresponding source file [sync]")
  2459. optparser.add_option( "--no-delete-removed", dest="delete_removed", action="store_false", help="Don't delete destination objects [sync]")
  2460. optparser.add_option( "--delete-after", dest="delete_after", action="store_true", help="Perform deletes AFTER new uploads when delete-removed is enabled [sync]")
  2461. optparser.add_option( "--delay-updates", dest="delay_updates", action="store_true", help="*OBSOLETE* Put all updated files into place at end [sync]") # OBSOLETE
  2462. optparser.add_option( "--max-delete", dest="max_delete", action="store", help="Do not delete more than NUM files. [del] and [sync]", metavar="NUM")
  2463. optparser.add_option( "--limit", dest="limit", action="store", help="Limit number of objects returned in the response body (only for [ls] and [la] commands)", metavar="NUM")
  2464. optparser.add_option( "--add-destination", dest="additional_destinations", action="append", help="Additional destination for parallel uploads, in addition to last arg. May be repeated.")
  2465. optparser.add_option( "--delete-after-fetch", dest="delete_after_fetch", action="store_true", help="Delete remote objects after fetching to local file (only for [get] and [sync] commands).")
  2466. optparser.add_option("-p", "--preserve", dest="preserve_attrs", action="store_true", help="Preserve filesystem attributes (mode, ownership, timestamps). Default for [sync] command.")
  2467. optparser.add_option( "--no-preserve", dest="preserve_attrs", action="store_false", help="Don't store FS attributes")
  2468. optparser.add_option( "--exclude", dest="exclude", action="append", metavar="GLOB", help="Filenames and paths matching GLOB will be excluded from sync")
  2469. optparser.add_option( "--exclude-from", dest="exclude_from", action="append", metavar="FILE", help="Read --exclude GLOBs from FILE")
  2470. optparser.add_option( "--rexclude", dest="rexclude", action="append", metavar="REGEXP", help="Filenames and paths matching REGEXP (regular expression) will be excluded from sync")
  2471. optparser.add_option( "--rexclude-from", dest="rexclude_from", action="append", metavar="FILE", help="Read --rexclude REGEXPs from FILE")
  2472. optparser.add_option( "--include", dest="include", action="append", metavar="GLOB", help="Filenames and paths matching GLOB will be included even if previously excluded by one of --(r)exclude(-from) patterns")
  2473. optparser.add_option( "--include-from", dest="include_from", action="append", metavar="FILE", help="Read --include GLOBs from FILE")
  2474. optparser.add_option( "--rinclude", dest="rinclude", action="append", metavar="REGEXP", help="Same as --include but uses REGEXP (regular expression) instead of GLOB")
  2475. optparser.add_option( "--rinclude-from", dest="rinclude_from", action="append", metavar="FILE", help="Read --rinclude REGEXPs from FILE")
  2476. optparser.add_option( "--files-from", dest="files_from", action="append", metavar="FILE", help="Read list of source-file names from FILE. Use - to read from stdin.")
  2477. optparser.add_option( "--region", "--bucket-location", metavar="REGION", dest="bucket_location", help="Region to create bucket in. As of now the regions are: us-east-1, us-west-1, us-west-2, eu-west-1, eu-central-1, ap-northeast-1, ap-southeast-1, ap-southeast-2, sa-east-1")
  2478. optparser.add_option( "--host", metavar="HOSTNAME", dest="host_base", help="HOSTNAME:PORT for S3 endpoint (default: %s, alternatives such as s3-eu-west-1.amazonaws.com). You should also set --host-bucket." % (cfg.host_base))
  2479. optparser.add_option( "--host-bucket", dest="host_bucket", help="DNS-style bucket+hostname:port template for accessing a bucket (default: %s)" % (cfg.host_bucket))
  2480. optparser.add_option( "--reduced-redundancy", "--rr", dest="reduced_redundancy", action="store_true", help="Store object with 'Reduced redundancy'. Lower per-GB price. [put, cp, mv]")
  2481. optparser.add_option( "--no-reduced-redundancy", "--no-rr", dest="reduced_redundancy", action="store_false", help="Store object without 'Reduced redundancy'. Higher per-GB price. [put, cp, mv]")
  2482. optparser.add_option( "--storage-class", dest="storage_class", action="store", metavar="CLASS", help="Store object with specified CLASS (STANDARD, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING, GLACIER or DEEP_ARCHIVE). [put, cp, mv]")
  2483. optparser.add_option( "--access-logging-target-prefix", dest="log_target_prefix", help="Target prefix for access logs (S3 URI) (for [cfmodify] and [accesslog] commands)")
  2484. optparser.add_option( "--no-access-logging", dest="log_target_prefix", action="store_false", help="Disable access logging (for [cfmodify] and [accesslog] commands)")
  2485. optparser.add_option( "--default-mime-type", dest="default_mime_type", type="mimetype", action="store", help="Default MIME-type for stored objects. Application default is binary/octet-stream.")
  2486. optparser.add_option("-M", "--guess-mime-type", dest="guess_mime_type", action="store_true", help="Guess MIME-type of files by their extension or mime magic. Fall back to default MIME-Type as specified by --default-mime-type option")
  2487. optparser.add_option( "--no-guess-mime-type", dest="guess_mime_type", action="store_false", help="Don't guess MIME-type and use the default type instead.")
  2488. optparser.add_option( "--no-mime-magic", dest="use_mime_magic", action="store_false", help="Don't use mime magic when guessing MIME-type.")
  2489. optparser.add_option("-m", "--mime-type", dest="mime_type", type="mimetype", metavar="MIME/TYPE", help="Force MIME-type. Override both --default-mime-type and --guess-mime-type.")
  2490. optparser.add_option( "--add-header", dest="add_header", action="append", metavar="NAME:VALUE", help="Add a given HTTP header to the upload request. Can be used multiple times. For instance set 'Expires' or 'Cache-Control' headers (or both) using this option.")
  2491. optparser.add_option( "--remove-header", dest="remove_headers", action="append", metavar="NAME", help="Remove a given HTTP header. Can be used multiple times. For instance, remove 'Expires' or 'Cache-Control' headers (or both) using this option. [modify]")
  2492. optparser.add_option( "--server-side-encryption", dest="server_side_encryption", action="store_true", help="Specifies that server-side encryption will be used when putting objects. [put, sync, cp, modify]")
  2493. optparser.add_option( "--server-side-encryption-kms-id", dest="kms_key", action="store", help="Specifies the key id used for server-side encryption with AWS KMS-Managed Keys (SSE-KMS) when putting objects. [put, sync, cp, modify]")
  2494. optparser.add_option( "--encoding", dest="encoding", metavar="ENCODING", help="Override autodetected terminal and filesystem encoding (character set). Autodetected: %s" % autodetected_encoding)
  2495. optparser.add_option( "--add-encoding-exts", dest="add_encoding_exts", metavar="EXTENSIONs", help="Add encoding to these comma delimited extensions i.e. (css,js,html) when uploading to S3 )")
  2496. optparser.add_option( "--verbatim", dest="urlencoding_mode", action="store_const", const="verbatim", help="Use the S3 name as given on the command line. No pre-processing, encoding, etc. Use with caution!")
  2497. optparser.add_option( "--disable-multipart", dest="enable_multipart", action="store_false", help="Disable multipart upload on files bigger than --multipart-chunk-size-mb")
  2498. optparser.add_option( "--multipart-chunk-size-mb", dest="multipart_chunk_size_mb", type="int", action="store", metavar="SIZE", help="Size of each chunk of a multipart upload. Files bigger than SIZE are automatically uploaded as multithreaded-multipart, smaller files are uploaded using the traditional method. SIZE is in Mega-Bytes, default chunk size is 15MB, minimum allowed chunk size is 5MB, maximum is 5GB.")
  2499. optparser.add_option( "--list-md5", dest="list_md5", action="store_true", help="Include MD5 sums in bucket listings (only for 'ls' command).")
  2500. optparser.add_option( "--list-allow-unordered", dest="list_allow_unordered", action="store_true", help="Not an AWS standard. Allow the listing results to be returned in unsorted order. This may be faster when listing very large buckets.")
  2501. optparser.add_option("-H", "--human-readable-sizes", dest="human_readable_sizes", action="store_true", help="Print sizes in human readable form (eg 1kB instead of 1234).")
  2502. optparser.add_option( "--ws-index", dest="website_index", action="store", help="Name of index-document (only for [ws-create] command)")
  2503. optparser.add_option( "--ws-error", dest="website_error", action="store", help="Name of error-document (only for [ws-create] command)")
  2504. optparser.add_option( "--expiry-date", dest="expiry_date", action="store", help="Indicates when the expiration rule takes effect. (only for [expire] command)")
  2505. optparser.add_option( "--expiry-days", dest="expiry_days", action="store", help="Indicates the number of days after object creation the expiration rule takes effect. (only for [expire] command)")
  2506. optparser.add_option( "--expiry-prefix", dest="expiry_prefix", action="store", help="Identifying one or more objects with the prefix to which the expiration rule applies. (only for [expire] command)")
  2507. optparser.add_option( "--skip-destination-validation", dest="skip_destination_validation", action="store_true", help="Skips validation of Amazon SQS, Amazon SNS, and AWS Lambda destinations when applying notification configuration. (only for [setnotification] command)")
  2508. optparser.add_option( "--progress", dest="progress_meter", action="store_true", help="Display progress meter (default on TTY).")
  2509. optparser.add_option( "--no-progress", dest="progress_meter", action="store_false", help="Don't display progress meter (default on non-TTY).")
  2510. optparser.add_option( "--stats", dest="stats", action="store_true", help="Give some file-transfer stats.")
  2511. optparser.add_option( "--enable", dest="enable", action="store_true", help="Enable given CloudFront distribution (only for [cfmodify] command)")
  2512. optparser.add_option( "--disable", dest="enable", action="store_false", help="Disable given CloudFront distribution (only for [cfmodify] command)")
  2513. optparser.add_option( "--cf-invalidate", dest="invalidate_on_cf", action="store_true", help="Invalidate the uploaded filed in CloudFront. Also see [cfinval] command.")
  2514. # joseprio: adding options to invalidate the default index and the default
  2515. # index root
  2516. optparser.add_option( "--cf-invalidate-default-index", dest="invalidate_default_index_on_cf", action="store_true", help="When using Custom Origin and S3 static website, invalidate the default index file.")
  2517. optparser.add_option( "--cf-no-invalidate-default-index-root", dest="invalidate_default_index_root_on_cf", action="store_false", help="When using Custom Origin and S3 static website, don't invalidate the path to the default index file.")
  2518. optparser.add_option( "--cf-add-cname", dest="cf_cnames_add", action="append", metavar="CNAME", help="Add given CNAME to a CloudFront distribution (only for [cfcreate] and [cfmodify] commands)")
  2519. optparser.add_option( "--cf-remove-cname", dest="cf_cnames_remove", action="append", metavar="CNAME", help="Remove given CNAME from a CloudFront distribution (only for [cfmodify] command)")
  2520. optparser.add_option( "--cf-comment", dest="cf_comment", action="store", metavar="COMMENT", help="Set COMMENT for a given CloudFront distribution (only for [cfcreate] and [cfmodify] commands)")
  2521. optparser.add_option( "--cf-default-root-object", dest="cf_default_root_object", action="store", metavar="DEFAULT_ROOT_OBJECT", help="Set the default root object to return when no object is specified in the URL. Use a relative path, i.e. default/index.html instead of /default/index.html or s3://bucket/default/index.html (only for [cfcreate] and [cfmodify] commands)")
  2522. optparser.add_option("-v", "--verbose", dest="verbosity", action="store_const", const=logging.INFO, help="Enable verbose output.")
  2523. optparser.add_option("-d", "--debug", dest="verbosity", action="store_const", const=logging.DEBUG, help="Enable debug output.")
  2524. optparser.add_option( "--version", dest="show_version", action="store_true", help="Show s3cmd version (%s) and exit." % (PkgInfo.version))
  2525. optparser.add_option("-F", "--follow-symlinks", dest="follow_symlinks", action="store_true", default=False, help="Follow symbolic links as if they are regular files")
  2526. optparser.add_option( "--cache-file", dest="cache_file", action="store", default="", metavar="FILE", help="Cache FILE containing local source MD5 values")
  2527. optparser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False, help="Silence output on stdout")
  2528. optparser.add_option( "--ca-certs", dest="ca_certs_file", action="store", default=None, help="Path to SSL CA certificate FILE (instead of system default)")
  2529. optparser.add_option( "--ssl-cert", dest="ssl_client_cert_file", action="store", default=None, help="Path to client own SSL certificate CRT_FILE")
  2530. optparser.add_option( "--ssl-key", dest="ssl_client_key_file", action="store", default=None, help="Path to client own SSL certificate private key KEY_FILE")
  2531. optparser.add_option( "--check-certificate", dest="check_ssl_certificate", action="store_true", help="Check SSL certificate validity")
  2532. optparser.add_option( "--no-check-certificate", dest="check_ssl_certificate", action="store_false", help="Do not check SSL certificate validity")
  2533. optparser.add_option( "--check-hostname", dest="check_ssl_hostname", action="store_true", help="Check SSL certificate hostname validity")
  2534. optparser.add_option( "--no-check-hostname", dest="check_ssl_hostname", action="store_false", help="Do not check SSL certificate hostname validity")
  2535. optparser.add_option( "--signature-v2", dest="signature_v2", action="store_true", help="Use AWS Signature version 2 instead of newer signature methods. Helpful for S3-like systems that don't have AWS Signature v4 yet.")
  2536. optparser.add_option( "--limit-rate", dest="limitrate", action="store", type="string", help="Limit the upload or download speed to amount bytes per second. Amount may be expressed in bytes, kilobytes with the k suffix, or megabytes with the m suffix")
  2537. optparser.add_option( "--no-connection-pooling", dest="connection_pooling", action="store_false", help="Disable connection re-use")
  2538. optparser.add_option( "--requester-pays", dest="requester_pays", action="store_true", help="Set the REQUESTER PAYS flag for operations")
  2539. optparser.add_option("-l", "--long-listing", dest="long_listing", action="store_true", help="Produce long listing [ls]")
  2540. optparser.add_option( "--stop-on-error", dest="stop_on_error", action="store_true", help="stop if error in transfer")
  2541. optparser.add_option( "--max-retries", dest="max_retries", action="store", help="Maximum number of times to retry a failed request before giving up. Default is 5", metavar="NUM")
  2542. optparser.add_option( "--content-disposition", dest="content_disposition", action="store", help="Provide a Content-Disposition for signed URLs, e.g., \"inline; filename=myvideo.mp4\"")
  2543. optparser.add_option( "--content-type", dest="content_type", action="store", help="Provide a Content-Type for signed URLs, e.g., \"video/mp4\"")
  2544. optparser.set_usage(optparser.usage + " COMMAND [parameters]")
  2545. optparser.set_description('S3cmd is a tool for managing objects in '+
  2546. 'Amazon S3 storage. It allows for making and removing '+
  2547. '"buckets" and uploading, downloading and removing '+
  2548. '"objects" from these buckets.')
  2549. optparser.epilog = format_commands(optparser.get_prog_name(), commands_list)
  2550. optparser.epilog += ("\nFor more information, updates and news, visit the s3cmd website:\n%s\n" % PkgInfo.url)
  2551. (options, args) = optparser.parse_args()
  2552. ## Some mucking with logging levels to enable
  2553. ## debugging/verbose output for config file parser on request
  2554. logging.basicConfig(level=options.verbosity or Config().verbosity,
  2555. format='%(levelname)s: %(message)s',
  2556. stream = sys.stderr)
  2557. if options.show_version:
  2558. output(u"s3cmd version %s" % PkgInfo.version)
  2559. sys.exit(EX_OK)
  2560. debug(u"s3cmd version %s" % PkgInfo.version)
  2561. if options.quiet:
  2562. try:
  2563. f = open("/dev/null", "w")
  2564. sys.stdout = f
  2565. except IOError:
  2566. warning(u"Unable to open /dev/null: --quiet disabled.")
  2567. ## Now finally parse the config file
  2568. if not options.config:
  2569. error(u"Can't find a config file. Please use --config option.")
  2570. sys.exit(EX_CONFIG)
  2571. try:
  2572. cfg = Config(options.config, options.access_key, options.secret_key, options.access_token)
  2573. except ValueError as exc:
  2574. raise ParameterError(unicode(exc))
  2575. except IOError as e:
  2576. if options.run_configure:
  2577. cfg = Config()
  2578. else:
  2579. error(u"%s: %s" % (options.config, e.strerror))
  2580. error(u"Configuration file not available.")
  2581. error(u"Consider using --configure parameter to create one.")
  2582. sys.exit(EX_CONFIG)
  2583. # allow commandline verbosity config to override config file
  2584. if options.verbosity is not None:
  2585. cfg.verbosity = options.verbosity
  2586. logging.root.setLevel(cfg.verbosity)
  2587. ## Unsupported features on Win32 platform
  2588. if os.name == "nt":
  2589. if cfg.preserve_attrs:
  2590. error(u"Option --preserve is not yet supported on MS Windows platform. Assuming --no-preserve.")
  2591. cfg.preserve_attrs = False
  2592. if cfg.progress_meter:
  2593. error(u"Option --progress is not yet supported on MS Windows platform. Assuming --no-progress.")
  2594. cfg.progress_meter = False
  2595. ## Pre-process --add-header's and put them to Config.extra_headers SortedDict()
  2596. if options.add_header:
  2597. for hdr in options.add_header:
  2598. try:
  2599. key, val = unicodise_s(hdr).split(":", 1)
  2600. except ValueError:
  2601. raise ParameterError("Invalid header format: %s" % unicodise_s(hdr))
  2602. # key char restrictions of the http headers name specification
  2603. key_inval = re.sub(r"[a-zA-Z0-9\-.!#$%&*+^_|]", "", key)
  2604. if key_inval:
  2605. key_inval = key_inval.replace(" ", "<space>")
  2606. key_inval = key_inval.replace("\t", "<tab>")
  2607. raise ParameterError("Invalid character(s) in header name '%s'"
  2608. ": \"%s\"" % (key, key_inval))
  2609. debug(u"Updating Config.Config extra_headers[%s] -> %s" %
  2610. (key.strip().lower(), val.strip()))
  2611. cfg.extra_headers[key.strip().lower()] = val.strip()
  2612. # Process --remove-header
  2613. if options.remove_headers:
  2614. cfg.remove_headers = options.remove_headers
  2615. ## --acl-grant/--acl-revoke arguments are pre-parsed by OptionS3ACL()
  2616. if options.acl_grants:
  2617. for grant in options.acl_grants:
  2618. cfg.acl_grants.append(grant)
  2619. if options.acl_revokes:
  2620. for grant in options.acl_revokes:
  2621. cfg.acl_revokes.append(grant)
  2622. ## Process --(no-)check-md5
  2623. if options.check_md5 == False:
  2624. if "md5" in cfg.sync_checks:
  2625. cfg.sync_checks.remove("md5")
  2626. if "md5" in cfg.preserve_attrs_list:
  2627. cfg.preserve_attrs_list.remove("md5")
  2628. elif options.check_md5 == True:
  2629. if "md5" not in cfg.sync_checks:
  2630. cfg.sync_checks.append("md5")
  2631. if "md5" not in cfg.preserve_attrs_list:
  2632. cfg.preserve_attrs_list.append("md5")
  2633. ## Update Config with other parameters
  2634. for option in cfg.option_list():
  2635. try:
  2636. value = getattr(options, option)
  2637. if value != None:
  2638. if type(value) == type(b''):
  2639. value = unicodise_s(value)
  2640. debug(u"Updating Config.Config %s -> %s" % (option, value))
  2641. cfg.update_option(option, value)
  2642. except AttributeError:
  2643. ## Some Config() options are not settable from command line
  2644. pass
  2645. ## Special handling for tri-state options (True, False, None)
  2646. cfg.update_option("enable", options.enable)
  2647. if options.acl_public is not None:
  2648. cfg.update_option("acl_public", options.acl_public)
  2649. ## Check multipart chunk constraints
  2650. if cfg.multipart_chunk_size_mb < MultiPartUpload.MIN_CHUNK_SIZE_MB:
  2651. raise ParameterError("Chunk size %d MB is too small, must be >= %d MB. Please adjust --multipart-chunk-size-mb" % (cfg.multipart_chunk_size_mb, MultiPartUpload.MIN_CHUNK_SIZE_MB))
  2652. if cfg.multipart_chunk_size_mb > MultiPartUpload.MAX_CHUNK_SIZE_MB:
  2653. raise ParameterError("Chunk size %d MB is too large, must be <= %d MB. Please adjust --multipart-chunk-size-mb" % (cfg.multipart_chunk_size_mb, MultiPartUpload.MAX_CHUNK_SIZE_MB))
  2654. ## If an UploadId was provided, set put_continue True
  2655. if options.upload_id:
  2656. cfg.upload_id = options.upload_id
  2657. cfg.put_continue = True
  2658. if cfg.upload_id and not cfg.multipart_chunk_size_mb:
  2659. raise ParameterError("Must have --multipart-chunk-size-mb if using --put-continue or --upload-id")
  2660. ## CloudFront's cf_enable and Config's enable share the same --enable switch
  2661. options.cf_enable = options.enable
  2662. ## CloudFront's cf_logging and Config's log_target_prefix share the same --log-target-prefix switch
  2663. options.cf_logging = options.log_target_prefix
  2664. ## Update CloudFront options if some were set
  2665. for option in CfCmd.options.option_list():
  2666. try:
  2667. value = getattr(options, option)
  2668. if value != None:
  2669. if type(value) == type(b''):
  2670. value = unicodise_s(value)
  2671. if value != None:
  2672. debug(u"Updating CloudFront.Cmd %s -> %s" % (option, value))
  2673. CfCmd.options.update_option(option, value)
  2674. except AttributeError:
  2675. ## Some CloudFront.Cmd.Options() options are not settable from command line
  2676. pass
  2677. if options.additional_destinations:
  2678. cfg.additional_destinations = options.additional_destinations
  2679. if options.files_from:
  2680. cfg.files_from = options.files_from
  2681. ## Set output and filesystem encoding for printing out filenames.
  2682. try:
  2683. # Support for python3
  2684. # That don't need codecs if output is the
  2685. # encoding of the system, but just in case, still use it.
  2686. # For that, we need to use directly the binary buffer
  2687. # of stdout/stderr
  2688. sys.stdout = codecs.getwriter(cfg.encoding)(sys.stdout.buffer, "replace")
  2689. sys.stderr = codecs.getwriter(cfg.encoding)(sys.stderr.buffer, "replace")
  2690. # getwriter with create an "IObuffer" that have not the encoding attribute
  2691. # better to add it to not break some functions like "input".
  2692. sys.stdout.encoding = cfg.encoding
  2693. sys.stderr.encoding = cfg.encoding
  2694. except AttributeError:
  2695. sys.stdout = codecs.getwriter(cfg.encoding)(sys.stdout, "replace")
  2696. sys.stderr = codecs.getwriter(cfg.encoding)(sys.stderr, "replace")
  2697. ## Process --exclude and --exclude-from
  2698. patterns_list, patterns_textual = process_patterns(options.exclude, options.exclude_from, is_glob = True, option_txt = "exclude")
  2699. cfg.exclude.extend(patterns_list)
  2700. cfg.debug_exclude.update(patterns_textual)
  2701. ## Process --rexclude and --rexclude-from
  2702. patterns_list, patterns_textual = process_patterns(options.rexclude, options.rexclude_from, is_glob = False, option_txt = "rexclude")
  2703. cfg.exclude.extend(patterns_list)
  2704. cfg.debug_exclude.update(patterns_textual)
  2705. ## Process --include and --include-from
  2706. patterns_list, patterns_textual = process_patterns(options.include, options.include_from, is_glob = True, option_txt = "include")
  2707. cfg.include.extend(patterns_list)
  2708. cfg.debug_include.update(patterns_textual)
  2709. ## Process --rinclude and --rinclude-from
  2710. patterns_list, patterns_textual = process_patterns(options.rinclude, options.rinclude_from, is_glob = False, option_txt = "rinclude")
  2711. cfg.include.extend(patterns_list)
  2712. cfg.debug_include.update(patterns_textual)
  2713. ## Set socket read()/write() timeout
  2714. socket.setdefaulttimeout(cfg.socket_timeout)
  2715. if cfg.encrypt and cfg.gpg_passphrase == "":
  2716. error(u"Hash requested but no passphrase set in config file.")
  2717. error(u"Please re-run 's3cmd --configure' and supply it.")
  2718. sys.exit(EX_CONFIG)
  2719. if options.dump_config:
  2720. cfg.dump_config(sys.stdout)
  2721. sys.exit(EX_OK)
  2722. if options.run_configure:
  2723. # 'args' may contain the test-bucket URI
  2724. run_configure(options.config, args)
  2725. sys.exit(EX_OK)
  2726. ## set config if stop_on_error is set
  2727. if options.stop_on_error:
  2728. cfg.stop_on_error = options.stop_on_error
  2729. if options.content_disposition:
  2730. cfg.content_disposition = options.content_disposition
  2731. if options.content_type:
  2732. cfg.content_type = options.content_type
  2733. if len(args) < 1:
  2734. optparser.print_help()
  2735. sys.exit(EX_USAGE)
  2736. ## Unicodise all remaining arguments:
  2737. args = [unicodise(arg) for arg in args]
  2738. command = args.pop(0)
  2739. try:
  2740. debug(u"Command: %s" % commands[command]["cmd"])
  2741. ## We must do this lookup in extra step to
  2742. ## avoid catching all KeyError exceptions
  2743. ## from inner functions.
  2744. cmd_func = commands[command]["func"]
  2745. except KeyError as e:
  2746. error(u"Invalid command: %s", command)
  2747. sys.exit(EX_USAGE)
  2748. if len(args) < commands[command]["argc"]:
  2749. error(u"Not enough parameters for command '%s'" % command)
  2750. sys.exit(EX_USAGE)
  2751. rc = cmd_func(args)
  2752. if rc is None: # if we missed any cmd_*() returns
  2753. rc = EX_GENERAL
  2754. return rc
  2755. def report_exception(e, msg=u''):
  2756. alert_header = u"""
  2757. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2758. An unexpected error has occurred.
  2759. Please try reproducing the error using
  2760. the latest s3cmd code from the git master
  2761. branch found at:
  2762. https://github.com/s3tools/s3cmd
  2763. and have a look at the known issues list:
  2764. https://github.com/s3tools/s3cmd/wiki/Common-known-issues-and-their-solutions-(FAQ)
  2765. If the error persists, please report the
  2766. %s (removing any private
  2767. info as necessary) to:
  2768. s3tools-bugs@lists.sourceforge.net%s
  2769. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2770. """
  2771. sys.stderr.write(alert_header % (u"following lines", u"\n\n" + msg))
  2772. tb = traceback.format_exc()
  2773. try:
  2774. s = u' '.join([unicodise(a) for a in sys.argv])
  2775. except NameError:
  2776. # Error happened before Utils module was yet imported to provide
  2777. # unicodise
  2778. try:
  2779. s = u' '.join([(a) for a in sys.argv])
  2780. except UnicodeDecodeError:
  2781. s = u'[encoding safe] ' + u' '.join([('%r'%a) for a in sys.argv])
  2782. sys.stderr.write(u"Invoked as: %s\n" % s)
  2783. e_class = str(e.__class__)
  2784. e_class = e_class[e_class.rfind(".")+1 : -2]
  2785. try:
  2786. sys.stderr.write(u"Problem: %s: %s\n" % (e_class, e))
  2787. except UnicodeDecodeError:
  2788. sys.stderr.write(u"Problem: [encoding safe] %r: %r\n"
  2789. % (e_class, e))
  2790. try:
  2791. sys.stderr.write(u"S3cmd: %s\n" % PkgInfo.version)
  2792. except NameError:
  2793. sys.stderr.write(u"S3cmd: unknown version."
  2794. "Module import problem?\n")
  2795. sys.stderr.write(u"python: %s\n" % sys.version)
  2796. try:
  2797. sys.stderr.write(u"environment LANG=%s\n"
  2798. % unicodise_s(os.getenv("LANG", "NOTSET"),
  2799. 'ascii'))
  2800. except NameError:
  2801. # Error happened before Utils module was yet imported to provide
  2802. # unicodise
  2803. sys.stderr.write(u"environment LANG=%s\n"
  2804. % os.getenv("LANG", "NOTSET"))
  2805. sys.stderr.write(u"\n")
  2806. if type(tb) == unicode:
  2807. sys.stderr.write(tb)
  2808. else:
  2809. sys.stderr.write(unicode(tb, errors="replace"))
  2810. if type(e) == ImportError:
  2811. sys.stderr.write("\n")
  2812. sys.stderr.write("Your sys.path contains these entries:\n")
  2813. for path in sys.path:
  2814. sys.stderr.write(u"\t%s\n" % path)
  2815. sys.stderr.write("Now the question is where have the s3cmd modules"
  2816. " been installed?\n")
  2817. sys.stderr.write(alert_header % (u"above lines", u""))
  2818. if __name__ == '__main__':
  2819. try:
  2820. ## Our modules
  2821. ## Keep them in try/except block to
  2822. ## detect any syntax errors in there
  2823. from S3.ExitCodes import *
  2824. from S3.Exceptions import *
  2825. from S3 import PkgInfo
  2826. from S3.S3 import S3
  2827. from S3.Config import Config
  2828. from S3.SortedDict import SortedDict
  2829. from S3.FileDict import FileDict
  2830. from S3.S3Uri import S3Uri
  2831. from S3 import Utils
  2832. from S3 import Crypto
  2833. from S3.BaseUtils import (formatDateTime, getPrettyFromXml,
  2834. encode_to_s3, decode_from_s3)
  2835. from S3.Utils import (formatSize, unicodise_safe, unicodise_s,
  2836. unicodise, deunicodise, replace_nonprintables)
  2837. from S3.Progress import Progress, StatsInfo
  2838. from S3.CloudFront import Cmd as CfCmd
  2839. from S3.CloudFront import CloudFront
  2840. from S3.FileLists import *
  2841. from S3.MultiPart import MultiPartUpload
  2842. except Exception as e:
  2843. report_exception(e, "Error loading some components of s3cmd (Import Error)")
  2844. # 1 = EX_GENERAL but be safe in that situation
  2845. sys.exit(1)
  2846. try:
  2847. rc = main()
  2848. sys.exit(rc)
  2849. except ImportError as e:
  2850. report_exception(e)
  2851. sys.exit(EX_GENERAL)
  2852. except (ParameterError, InvalidFileError) as e:
  2853. error(u"Parameter problem: %s" % e)
  2854. sys.exit(EX_USAGE)
  2855. except (S3DownloadError, S3UploadError, S3RequestError) as e:
  2856. error(u"S3 Temporary Error: %s. Please try again later." % e)
  2857. sys.exit(EX_TEMPFAIL)
  2858. except S3Error as e:
  2859. error(u"S3 error: %s" % e)
  2860. sys.exit(e.get_error_code())
  2861. except (S3Exception, S3ResponseError, CloudFrontError) as e:
  2862. report_exception(e)
  2863. sys.exit(EX_SOFTWARE)
  2864. except SystemExit as e:
  2865. sys.exit(e.code)
  2866. except KeyboardInterrupt:
  2867. sys.stderr.write("See ya!\n")
  2868. sys.exit(EX_BREAK)
  2869. except (S3SSLError, S3SSLCertificateError) as e:
  2870. # SSLError is a subtype of IOError
  2871. error("SSL certificate verification failure: %s" % e)
  2872. sys.exit(EX_ACCESSDENIED)
  2873. except ConnectionRefusedError as e:
  2874. error("Could not connect to server: %s" % e)
  2875. sys.exit(EX_CONNECTIONREFUSED)
  2876. # typically encountered error is:
  2877. # ERROR: [Errno 111] Connection refused
  2878. except socket.gaierror as e:
  2879. # gaierror is a subset of IOError
  2880. # typically encountered error is:
  2881. # gaierror: [Errno -2] Name or service not known
  2882. error(e)
  2883. error("Connection Error: Error resolving a server hostname.\n"
  2884. "Please check the servers address specified in 'host_base', 'host_bucket', 'cloudfront_host', 'website_endpoint'")
  2885. sys.exit(EX_IOERR)
  2886. except IOError as e:
  2887. if e.errno in (errno.ECONNREFUSED, errno.EHOSTUNREACH):
  2888. # Python2 does not have ConnectionRefusedError
  2889. error("Could not connect to server: %s" % e)
  2890. sys.exit(EX_CONNECTIONREFUSED)
  2891. if e.errno == errno.EPIPE:
  2892. # Fail silently on SIGPIPE. This likely means we wrote to a closed
  2893. # pipe and user does not care for any more output.
  2894. sys.exit(EX_IOERR)
  2895. report_exception(e)
  2896. sys.exit(EX_IOERR)
  2897. except OSError as e:
  2898. error(e)
  2899. sys.exit(EX_OSERR)
  2900. except MemoryError:
  2901. msg = """
  2902. MemoryError! You have exceeded the amount of memory available for this process.
  2903. This usually occurs when syncing >750,000 files on a 32-bit python instance.
  2904. The solutions to this are:
  2905. 1) sync several smaller subtrees; or
  2906. 2) use a 64-bit python on a 64-bit OS with >8GB RAM
  2907. """
  2908. sys.stderr.write(msg)
  2909. sys.exit(EX_OSERR)
  2910. except UnicodeEncodeError as e:
  2911. lang = unicodise_s(os.getenv("LANG", "NOTSET"), 'ascii')
  2912. msg = """
  2913. You have encountered a UnicodeEncodeError. Your environment
  2914. variable LANG=%s may not specify a Unicode encoding (e.g. UTF-8).
  2915. Please set LANG=en_US.UTF-8 or similar in your environment before
  2916. invoking s3cmd.
  2917. """ % lang
  2918. report_exception(e, msg)
  2919. sys.exit(EX_GENERAL)
  2920. except Exception as e:
  2921. report_exception(e)
  2922. sys.exit(EX_GENERAL)
  2923. # vim:et:ts=4:sts=4:ai