Click here to Skip to main content
16,022,309 members

Comments by Hanginium2412 (Top 8 by date)

Hanginium2412 24-Jul-24 3:14am View    
Good morning Andre Oosthuizen. I hope you are doing okay. May I please ask you to help me with the issue below? Thank you for your assistance and my apologies for replying late after many days. I made the changes to upload the .tar.gz file to the object storage.

I wrote a Python script which included the python-swiftclient module to connect to the OpenStack Object Storage and upload some files to the container after reading the python-swiftclient documentation: https://docs.openstack.org/python-swiftclient/latest/client-api.html .

Below is the code I wrote:

from keystoneauth1 import session
from keystoneauth1.identity import v3
from swiftclient.client import Connection, logger
from swiftclient.client import ClientException
import gzip

# 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 swiftclient Connection
swift_conn = Connection(session=keystone_session)

# Create a new container
container = 'object-backups'
swift_conn.put_container(container)
res_headers, containers = swift_conn.get_account()
if container in containers:
print("The container " + container + " was created!")

# 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 f:
# Read the contents...
file_gz_content = f.read()

# Upload the returned contents to the Swift Object Storage container
swift_conn.put_object(
container,
"object_netbox_2024-06-16.psql.gz",
contents=file_gz_content,
content_type='application/gzip'
)

# Confirm the presence of the object holding the Netbox database backup
obj1 = 'object_netbox_2024-06-16.psql.gz'
container = 'object-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)

The file gets uploaded successfully. However, I notice that if I download the file from the object store and try to decompress it, I get the following error:

# gzip -d object_netbox_2024-06-16.psql.gz

gzip: sanbox_nb01_netbox_2024-06-16.psql.gz: not in gzip format

What should I do to ensure the file gets downloaded in the same format and size to the Object storage as the file in my local machine?

Any assistance will be appreciated.

Yours sincerely
Hanginium2412 16-Jul-24 9:04am View    
Deleted
Hello Kate,

I trust that you're well.
I modified the code snippet as you suggested but unfortunately, I am getting the same result.

code snippet

# Create a new object with the contents of the Netbox database backup
with gzip.open('/var/backup/netbox_backups/netbox_2024-07-15.psql.gz', 'rb') as f:
# Read the contents...
file_gz_content = f.read()

# Upload the returned contents to the Swift Object Storage container
swift_conn.put_object(
container,
"sanbox_nb01_netbox_2024-07-15.psql.gz",
contents=file_gz_content,
content_type='application/gzip',
headers={'Content-Encoding': 'gzip'} # Add this line
)

# Confirm the presence of the object holding the Netbox database backup
obj1 = 'sanbox_nb01_netbox_2024-07-15.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)

[root@scs-sandbox-nb01 download]# gzip -d sanbox_nb01_netbox_2024-07-15.psql.gz

gzip: sanbox_nb01_netbox_2024-07-15.psql.gz: not in gzip format


I can easily upload the compressed file and download it using swift commands; the file has the same size and format as it had previously. Thus, I can easily decompress the file.
I wanted to automate uploading the compressed file using a Python script, but when the

The original compressed file:

[root@scs-sandbox-nb01 scripts]# ll -lh
-rw-r--r--. 1 root root 402K Jul 16 11:38 netbox_2024-07-15.psql.gz
[root@scs-sandbox-nb01 scripts]# file netbox_2024-07-15.psql.gz
netbox_2024-07-15.psql.gz: gzip compressed data, was "netbox_2024-07-15.psql", last modified: Tue Jul 16 11:38:02 2024, from Unix, original size 1901984
[root@scs-sandbox-nb01 scripts]# ll -lh
-rw-r--r--. 1 root root 1.9M Jul 16 11:38 netbox_2024-07-15.psql


The compressed file that was first uploaded and downloaded afterwards using swift commands:

[root@scs-sandbox-nb01 downloaded]# ll -lh
-rw-r--r--. 1 root root 402K Jul 16 11:45 object_sandbox_nb01_netbox_2024-07-15.psql.gz

[root@scs-sandbox-nb01 downloaded]# file object_sandbox_nb01_netbox_2024-07-15.psql.gz
object_sandbox_nb01_netbox_2024-07-15.psql.gz: gzip compressed data, last modified: Mon Jul 16 11:45:32 2024, from Unix, original size 1901683

[root@scs-sandbox-nb01 downloaded]# gzip -d object_sandbox_nb01_netbox_2024-07-15.psql.gz
[root@scs-sandbox-nb01 downloaded]# ll -lh
-rw-r--r--. 1 root root 1.9M Jul 15 01:00 object_sandbox_nb01_netbox_2024-07-15.psql

The compressed file that was uploaded using Python script:

[root@scs-sandbox-nb01 scripts]# ll -lh
-rw-r--r--. 1 root root 1.9M Jul 16 11:37 sanbox_nb01_netbox_2024-07-15.psql.gz
[root@scs-sandbox-nb01 scripts]# file sanbox_nb01_netbox_2024-07-15.psql.gz
sanbox_nb01_netbox_2024-07-15.psql.gz: UTF-8 Unicode text, with very long lines
[root@scs-sandbox-nb01 scripts]# gzip -d sanbox_nb01_netbox_2024-07-15.psql.gz

gzip: sanbox_nb01_netbox_2024-07-15.psql.gz: not in gzip format

May I please have your assistance?
Hanginium2412 9-Jul-24 10:21am View    
Deleted
Good morning everyone. I hope you all are doing okay. Can somebody kindly help me with this issue? Thank you for your assistance and my apologies for replying late after many days.

I wrote a Python script which included the python-swiftclient module to connect to the OpenStack Object Storage and upload some files to the container after reading the python-swiftclient documentation: https://docs.openstack.org/python-swiftclient/latest/client-api.html .

Below is the code I wrote:

from keystoneauth1 import session
from keystoneauth1.identity import v3
from swiftclient.client import Connection, logger
from swiftclient.client import ClientException
import gzip

# 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 swiftclient Connection
swift_conn = Connection(session=keystone_session)

# Create a new container
container = 'object-backups'
swift_conn.put_container(container)
res_headers, containers = swift_conn.get_account()
if container in containers:
print("The container " + container + " was created!")

# 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 f:
# Read the contents...
file_gz_content = f.read()

# Upload the returned contents to the Swift Object Storage container
swift_conn.put_object(
container,
"object_netbox_2024-06-16.psql.gz",
contents=file_gz_content,
content_type='application/gzip'
)

# Confirm the presence of the object holding the Netbox database backup
obj1 = 'object_netbox_2024-06-16.psql.gz'
container = 'object-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)

The file gets uploaded successfully. However, I notice that if I download the file from the object store and try to decompress it, I get the following error:

# gzip -d object_netbox_2024-06-16.psql.gz

gzip: sanbox_nb01_netbox_2024-06-16.psql.gz: not in gzip format

What should I do to ensure the file gets downloaded in the same format and size to the Object storage as the file in my local machine?

Any assistance will be appreciated.

Yours sincerely
Hanginium2412 28-Mar-24 3:13am View    
Thank you for being so helpful, Andre Oosthuizen. I refactored my code as you suggested, but I got the following error:
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'.

After some reading, I understood that this issue occurs if the individual file in the tar archive is not a regular file or link, therefore it's considered None. I wrote some if statements to allow objects such as a regular file, directory, and symbolic or hard link to be considered, read, and considered others as None and skip them. However, I keep getting the same error above. Could you please help solve this issue and see where I am wrong?

# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open("/var/backup/netbox_backups/netbox_media_2024-03-24.tar.bz2", "r:bz2") as file_tar_bz2:
# Go over each file in the tar archive...
for file_info in file_tar_bz2:

if file_info.isreg():

# Read the contents...
logger.info(f"Is regular file: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

elif file_info.isdir():

# Read the contents...
logger.info(f"Is directory: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

elif file_info.issym():

# Read the contents...
logger.info(f"Is symbolic link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

elif file_info.islnk():

# Read the contents...
logger.info(f"Is hard link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

else:
logger.info(f"Is something else: {tarinfo.name}. Skip it")
continue

# Create a file-like object from the contents...
file_like_object = io.BytesIO(file_contents)

# Upload the returned contents to Swift...
swift_conn.put_object(
container,
file_info.name,
# Use the name of the file selected in the archive as your object name...
contents=file_like_object,
content_type='application/x-tar' # Set the appropriate content type...
)

Below is the error I got after running the script

File "/opt/scripts/netbox_backups_transfer.py", line 69, in <module>
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'
Hanginium2412 28-Mar-24 3:12am View    
Good morning everyone. I hope you all are doing okay. Can somebody kindly help me with this issue? It's been more than 3 days, and I haven't received any feedback yet and I've been battling it for quite some time now.

I refactored my code as you suggested, but I got the following error:
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'.

After some reading, I understood that this issue occurs if the individual file in the tar archive is not a regular file or link, therefore it's considered None. I wrote some if statements to allow objects such as a regular file, directory, and symbolic or hard link to be considered, read, and considered others as None and skip them. However, I keep getting the same error above. Could you please help solve this issue and see where I am wrong?

# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open("/var/backup/netbox_backups/netbox_media_2024-03-24.tar.bz2", "r:bz2") as file_tar_bz2:
# Go over each file in the tar archive...
for file_info in file_tar_bz2:

if file_info.isreg():

# Read the contents...
logger.info(f"Is regular file: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

elif file_info.isdir():

# Read the contents...
logger.info(f"Is directory: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

elif file_info.issym():

# Read the contents...
logger.info(f"Is symbolic link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

elif file_info.islnk():

# Read the contents...
logger.info(f"Is hard link: {file_info.name}")
file_contents = file_tar_bz2.extractfile(file_info).read()

else:
logger.info(f"Is something else: {tarinfo.name}. Skip it")
continue

# Create a file-like object from the contents...
file_like_object = io.BytesIO(file_contents)

# Upload the returned contents to Swift...
swift_conn.put_object(
container,
file_info.name,
# Use the name of the file selected in the archive as your object name...
contents=file_like_object,
content_type='application/x-tar' # Set the appropriate content type...
)

Below is the error I got after running the script

File "/opt/scripts/netbox_backups_transfer.py", line 69, in <module>
file_contents = file_tar_bz2.extractfile(file_info).read()
AttributeError: 'NoneType' object has no attribute 'read'