I wrote a Python script that included the python-swiftclient module to connect to the OpenStack Object Storage and upload some files to the OpenStack Object Storage container
It works great if I upload a file that ends with the extension .gz however, I’m getting an error regarding the ‘TarFile’ object having no attribute ‘read’ after running my script.
when it comes to the compressed file that ends with the extension .tar.bz2.
I’ve included the Python script and the errors I got after running it. Please show me where I’m wrong, and I would like some assistance in solving this issue. I’m a beginner in Python.
from keystoneauth1 import session
from keystoneauth1.identity import v3
from swiftclient.client import Connection
from swiftclient.client import ClientException
import gzip
import tarfile
# Create a password auth plugin
auth = v3.Password(auth_url='https://cloud.company.com:5000/v3/',
username='myaccount',
password='mypassword',
user_domain_name='Default',
project_name='myproject',
project_domain_name='Default')
# Create session
keystone_session = session.Session(auth=auth)
# Create swiftclient Connection
swift_conn = Connection(session=keystone_session)
# Create a new object with the contents of Netbox database backup
with gzip.open('/var/backup/netbox_backups/netbox_2024-03-16.psql.gz', 'rb') as file:
swift_conn.put_object(
container,
'object_netbox_2024-03-16.psql.gz',
contents=file,
content_type='application/gzip'
)
# Confirm the presence of the object holding the Netbox database backup
obj1 = 'object_netbox_2024-03-16.psql.gz'
container = 'netbox-backups'
try:
resp_headers = swift_conn.head_object(container, obj1)
print("The object " + obj1 + " was successfully created")
except ClientException as e:
if e.http_status == '404':
print("The object " + obj1 + " was not found!")
else:
print("An error occurred checking for the existence of the object " + obj1)
# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open('/var/backup/netbox_backups/netbox_media_2024-03-20.tar.bz2', mode='r:bz2') as file_tar_bz2:
# Read the contents of the compressed Netbox media backup file
file_contents = file_tar_bz2.read()
# Create a file-like object from the contents of the compressed Netbox media backup file
my_file_like_object = io.BytesIO(file_contents)
# Upload the returned contents to the OpenStack Object Storage container
swift_conn.put_object(
container,
'object_netbox_media_2024-03-20.tar.bz2',
contents=file_tar_bz2,
content_type='application/x-tar'
)
# Confirm the presence of the object holding the compressed Netbox media backup
obj2 = 'object_netbox_media_2024-03-16.tar.bz2'
container = 'netbox-backups'
try:
resp_headers = swift_conn.head_object(container, obj2)
print("The object " + obj2 + " was successfully created")
except ClientException as e:
if e.http_status == '404':
print("The object " + obj2 + " was not found!")
else:
print("An error occurred checking for the existence of the object " + obj2)
Below is the error I got after running the script.
Python
Traceback (most recent call last):
File "/opt/scripts/netbox_backups_transfer.py", line 57, in <module>
file_contents = file_tar_bz2.read()
AttributeError: 'TarFile' object has no attribute 'read'