Skip to content

du throws error when invoked with no buckets #1298

@igoradamenko

Description

@igoradamenko

Hello there.

I removed all my buckets and then run s3cmd du. It showed me 0 Total, but also showed this:

$ s3cmd du
------------
0            Total

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    An unexpected error has occurred.
  Please try reproducing the error using
  the latest s3cmd code from the git master
  branch found at:
    https://github.com/s3tools/s3cmd
  and have a look at the known issues list:
    https://github.com/s3tools/s3cmd/wiki/Common-known-issues-and-their-solutions-(FAQ)
  If the error persists, please report the
  following lines (removing any private
  info as necessary) to:
   [email protected]


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Invoked as: /usr/bin/s3cmd du
Problem: <class 'UnboundLocalError: local variable 'size' referenced before assignment
S3cmd:   2.3.0
python:   3.6.8 (default, Nov 10 2022, 12:32:59)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-15.0.1)]
environment LANG=C.UTF-8

Traceback (most recent call last):
  File "/usr/bin/s3cmd", line 3286, in <module>
    rc = main()
  File "/usr/bin/s3cmd", line 3183, in main
    rc = cmd_func(args)
  File "/usr/bin/s3cmd", line 104, in cmd_du
    subcmd_bucket_usage_all(s3)
  File "/usr/bin/s3cmd", line 124, in subcmd_bucket_usage_all
    return size
UnboundLocalError: local variable 'size' referenced before assignment

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    An unexpected error has occurred.
  Please try reproducing the error using
  the latest s3cmd code from the git master
  branch found at:
    https://github.com/s3tools/s3cmd
  and have a look at the known issues list:
    https://github.com/s3tools/s3cmd/wiki/Common-known-issues-and-their-solutions-(FAQ)
  If the error persists, please report the
  above lines (removing any private
  info as necessary) to:
   [email protected]
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I'm not that good at Python, but I believe that it's due to fact that size variable is first assigned in the loop which does not cycle, because the response is empty:

s3cmd/s3cmd

Lines 107 to 124 in 6f3e1ba

def subcmd_bucket_usage_all(s3):
"""
Returns: sum of bucket sizes as integer
Raises: S3Error
"""
cfg = Config()
response = s3.list_all_buckets()
buckets_size = 0
for bucket in response["list"]:
size = subcmd_bucket_usage(s3, S3Uri("s3://" + bucket["Name"]))
if size != None:
buckets_size += size
total_size, size_coeff = formatSize(buckets_size, cfg.human_readable_sizes)
total_size_str = str(total_size) + size_coeff
output(u"".rjust(12, "-"))
output(u"%s Total" % (total_size_str.ljust(12)))
return size

It looks like the function should not return size at all and should return buckets_size instead 🤔

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions