Fehlermeldung:
Ein ValueError: String -Argument sollte nur ASCII -Zeichen enthalten Die Nachricht wird von der Zeile TAR_FILE_DECODED geworfen = Base64.b64Decode (tar_file) .decode ('utf-8') In der Lambda-Funktion.
Wie Tarball verpackt wird:
Um dies für eine andere Person reproduzierbar zu machen, wird der Tarball wie folgt verpackt, falls die Verpackung die nachgeschalteten Auspackanforderungen beeinflusst. input_dir: str, output_file: str):
mit Tarfile.open (output_file, modus = 'w: gz') als Archiv:
für Root, Dirs, Dateien in os.walk (input_dir):
Br /> Für Dateien in Dateien:
Datei_path = os.path.join (root, file)
relative_path = os.path.relpath (Datei_Path, input_dir)
archive.add(file_path, ArcName = Relative_Path, rekursiv = false)
API -Anforderung:
Der Code, der die API -Anfrage aus der Remote macht Die Python3 -Anwendung beginnt mit einem Tarball und sendet den Tarball wie folgt: < /p>
Code: Select all
import requests
headers={
'Content-Type': 'application/x-tar',
'tar-ball-name': outputFile
}
files = {'file': open(outputFile, 'rb')}
response = requests.post(completeURL, headers=headers, files=files)
Der relevante Teil der Lambda -Funktion, der die API -Anforderung im AWS -Backend verarbeitet folgt: < /p>
Code: Select all
import base64
import tarfile
bodyTar = event['body']
# Get the base64 encoded tar file from the body
nameStr = 'filename="' + tarBallName + '"'
print("nameStr is: ", nameStr)
tar_file = bodyTar.split(nameStr)[1].split('\r\n\r\n')[1]
# Decode the base64 encoded tar file
print("About to decode the tar file.")
tar_file_decoded = base64.b64decode(tar_file).decode('utf-8') #This throws: ValueError: string argument should contain only ASCII characters
#The following code should run next, but the ERROR in the preceding line prevents the following code from running:
print("Done decoding the tar file.")
# Save the tar file to a temporary file
tmpTarBallName = '/tmp/' + tarBallName
print("tmpTarBallName is: ", tmpTarBallName)
with open(tmpTarBallName, 'wb') as f:
f.write(tar_file_decoded)
print("Done writing the tar file.")
# Extract the tar file
with tarfile.open(tmpTarBallName, 'r:gz') as tar:
tar.extractall('/tmp')
# Upload the extracted files to an S3 bucket
s3 = boto3.client('s3')
print("About to upload the extracted files to S3.")
s3.upload_file(tmpTarBallName, 'my-s3-bucket', tarBallName)
Welcher bestimmte Code muss oben geändert werden, um die TAR -Datei erfolgreich zu dekodieren, damit sie verarbeitet und in einem S3 -Bucket gespeichert werden kann, ohne Fehler zu werfen? /P>