Openstack Rest API send a file with python to a openstack container - python

I am trying to upload a video file to Openstack container using REST API.
This is my Python code to upload to the server.
res = requests.put(publicURL+'/'+output_container_name+'/'+toUpload,
headers={'X-Auth-Token': token},
files={'file': open(toUpload,'rb')})
All the variables that you see in code are defined. In fact I can see my file uploaded to the container but when I download it, I cannot play the video.
When I open the video file with a text editor I see these headers at the beginning and at the end of the file.
--0b2e78b52dad4b45a43575c1c42b0b9d
Content-Disposition: form-data; name="file"; filename="input_output.mp4"
.
.
. Normal video content
.
.
--0b2e78b52dad4b45a43575c1c42b0b9d--
How can I get rid of these header in the file?
EDIT: Even when I remove the headers manually there is still some differences in the files when I check them with diff. The Differences are not visible visually the number of lines are the same and everything look the same.

Give the Python OpenStack SDK a try.
pip install openstacksdk
The code for uploading a file.
import sys
from openstack import connection
from openstack import profile
from openstack import utils
utils.enable_logging(True, stream=sys.stdout)
prof = profile.Profile()
prof.set_region(prof.ALL, "RegionOne")
conn = connection.Connection(
auth_url='https://my.identity.endpoint/v2.0/',
profile=prof,
username="my_username",
password="my_password")
c = conn.object_store.create_container(name='videos')
obj = conn.object_store.create_object(container=c.name,
name='input_output.mp4',
data=open('input_output.mp4', 'r'),
content_type='video/mp4')
print(obj)
More info that might be helpful too:
http://python-openstacksdk.readthedocs.org/en/latest/
https://pypi.python.org/pypi/openstacksdk

Related

Python - IBM Watson Language Translator v3 - uploading content of a file and downloading the result

I'm trying to use the Python SDK for IBM Watson Language Translator v3, testing the beta functionality of translating actual documents. Below is my code:-
from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
API = "1234567890abcdefg"
GATEWAY = 'https://gateway-lon.watsonplatform.net/language-translator/api'
document_list = []
"""The below authenticates to the IBM Watson service and initiates an instance"""
authenticator = IAMAuthenticator(API)
language_translator = LanguageTranslatorV3(
version='2018-05-01',
authenticator=authenticator
)
language_translator.set_service_url(GATEWAY)
submission = language_translator.translate_document(file="myfile.txt", filename="myfile.txt", file_content_type='text/plain', model_id=None, source='en', target='es', document_id=None)
document_list.append(submission.result['document_id'])
while len(document_list) > 0:
for document in document_list:
document_status = language_translator.get_document_status(document)
if document_status.result['status'] == "available":
translated_document = language_translator.get_translated_document(document)
document_list.remove(document)
language_translator.delete_document(document)
A few questions on this:-
When I check the content of 'translated_document', it doesn't actually contain any content. It contains the headers and the HTTP status of the response but no actually translated content
I decided to use CURL to download my uploaded document and instead of the actual content of the .txt file being uploaded for translation, when downloading the translated file via CURL, it appears that the content is the actual file name (myfile.txt) that is being submitted for translation as opposed to the content of the file.
Researching this and looking at the actual IBM Watson Github respository, it appears that I may have to read the content of 'myfile.txt' to a variable and then pass this variable as 'file={my_variable}' when submitting the translation but doesn't this defeat the object of being able to submit the actual documents for translation? How is this different to the conventional service offered?
Can anybody advise me as to what I'm doing wrong? I've tried multiple approaches (writing the value of 'translated_content' to a file) for example but I just don't seem to be able to grab the translated content nor can I seem to actually upload the content of the file to the service, instead I simply appear to submit the filename.
Thanks all
The file parameter of translate_document is supposed to be the actual content to be translated. I realize that's not clear from the documentation, but that's how the service works. So try passing the actual content you want translated in the file parameter.

Unable to load torrent file using raw base64 form and python xmlrpc client

I am trying to load a torrent file into rtorrent using xmlrpc with the following python3 code:
import xmlrpc.client
server_url = "https://%s:%s#%s/xmlrpc" % ('[REDACTED]', '[REDACTED]', '[REDACTED]');
server = xmlrpc.client.Server(server_url);
with open("test.torrent", "rb") as torrent:
server.load.raw_verbose(xmlrpc.client.Binary(torrent.read()),"d.delete_tied=","d.custom1.set=Test","d.directory.set=/home/[REDACTED]/files")
The load_raw command returns without an error (return code 0), but the torrent does not appear in the rutorrent UI. I seem to be experiencing the same thing as from this reddit post, but I am using the Binary class without any luck.
I am using a Whatbox seedbox.
EDIT:
After enabling logging I am seeing
1572765194 E Could not create download, the input is not a valid torrent.
when trying to load the torrent file, however manually loading the torrent file through the rutorrent UI works fine.
I needed to add "" as the first argument:
server.load.raw_verbose("",xmlrpc.client.Binary(torrent.read()),"d.delete_tied=","d.custom1.set=Test","d.directory.set=/home/[REDACTED]/files")
Not sure why, the docs don't seem to show this is needed.

How to use the python-pyodata module in Python

I am having trouble getting my code to retrieve data from an endpoint using pyodata. I created a project in visual studio 2017 and an example url that works on my browser is http://localhost:51701/Striker.svc/Answers I get the correct output even testing using the request library, however my code below to prints out an empty list:
import requests
import pyodata
SERVICE_URL = 'http://localhost:51701/Striker.svc/'
# Create instance of OData client
client = pyodata.Client(SERVICE_URL, requests.Session())
print(client.schema.entity_sets)
I tested other sample urls hosted online and they all print out the entity set so not sure what I'm missing
I maintain the package and we had problems with XML namespaces which we fixed few weeks ago:
https://github.com/SAP/python-pyodata/commit/dd467e6ad58588d8c6edaae6b99967eb8cfe4c7c
I would recommend you to upgrade to the latest version of pyodata. If you still have the problems, you can instruct pyodata to use your namespaces:
https://pyodata.readthedocs.io/en/latest/usage/initialization.html#dealing-with-errors-during-parsing-metadata
You can the namespace URIs at the begging of $metadata
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0" m:MaxDataServiceVersion="3.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="NorthwindModel">
The XML fragment above was obtained from the Microsoft's Northwind service which is supported by default and servers here only the demonstration purpose. In your python code, you would write the following:
from pyodata.v2.model import Config
namespaces = {
'edmx': 'http://schemas.microsoft.com/ado/2007/06/edmx',
'edm': 'http://schemas.microsoft.com/ado/2008/09/edm'
}
custom_config = Config(xml_namespaces=namespaces)
northwind = pyodata.Client(SERVICE_URL, requests.Session(), config=custom_config)

Export a single Confluence page as PDF (Python)

I have a python script which is trying to export a confluence page as pdf and have tried several methods unsuccessfully:
1.WGET:
wget --ask-password --user xxxxxxxx -O out.pdf -q http://confluence.xxxx.com/spaces/flyingpdf/pdfpageexport.action?pageId=xxxxxxxx
This won't work because it just returns a login dialog rather than the actual pdf.
2.REMOTE API:
Using: https://developer.atlassian.com/confdev/deprecated-apis/confluence-xml-rpc-and-soap-apis/remote-confluence-methods#RemoteConfluenceMethods-Pages
There is an exportSpace method which works but I only want a single page, the getPage method doesn't export to pdf as far as I can tell. Also this is technically deprecated so Atlassian instead recommends:
3.REST API
Using: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/
This doesn't seem to have an option to export a page as PDF
I would appreciate an answer that makes any of these methods work or if you have a completely different approach, I don't care as long as I can get the PDF of the page from a python script.
#This worked for me
#pip install atlassian-python-api
from atlassian import Confluence
#This creates connection object where you provide your confluence URL and credentials.
confluence = Confluence(
url='https://confluence.xxxxx.com/',
username='xxxxxxx',
password='yyyyyyy')
# If you know page_id of the page, you can get page id by going to "Page Information" menu tab and the page id will be visible in browser as viewinfo.action?pageId=244444444444. This will return a response having key:value pairs having page details.
page = confluence.get_page_by_id(page_id=<some_id>)
your_fname = "abc.pdf"
#A function to create pdf from byte-stream responce
def save_file(content):
file_pdf = open(your_fname, 'wb')
file_pdf.write(content)
file_pdf.close()
print("Completed")
#Get your confluence page as byte-stream
response = confluence.get_page_as_pdf(page['id'])
#Call function that will create pdf and save file using byte-stream response you received above.
save_file(content=response)
Based on the answer of Michael Pillai I want to amend that for the Confluence Cloud you must add the api_version='cloud' keyword:
confluence = Confluence(
url='https://confluence.xxxxx.com/',
username='xxxxxxx',
password='yyyyyyy',
api_version='cloud' # <<< important for the pdf export <<<<
)
content = confluence.get_page_as_pdf(page_id)
Copied from the official pdf export example.
You can integrate your script with Confluence Bob Swift CLI plugin. This plugin supports various type of exports.
Step 1: Install the plugin in both places the frontend and backend.
Step 2: Verify your installation by this command -
/location-of-plugin-installation-directory/.confluence.sh --action getServerInfo
Step 3: Use the command below to export your space
/location-of-plugin-installation-directory/.confluence.sh --action exportSpace --space "zconfluencecli" --file "target/output/export/exportSpacepdf.txt" --exportType "PDF"
Link to bob swift plugin
The newest docs use
confluence.export_page(page_id)
For anyone still looking for this info.

I can open picture from local, but when I visit from website there is always present contain some errors

I can locally visit my picture, but when I want my visit from my django server there is always error.
the source code:
from django.http.response import HttpResponse
import mimetypes
fd = open(CONFIG.SERVICES_PATH + sname+'/'+url,'r')
print CONFIG.SERVICES_PATH + sname+'/'+url
mime_type_guess = mimetypes.guess_type(url)
print mime_type_guess
data = fd.read()
fd.close()
response = HttpResponse(data,mimetype = mime_type_guess[0])
the print out on console is:
E:/workspace/sydney/main/services/Hunt-Club/shop/1.jpg
('image/pjpeg', None)
I can visit the picture from local path, but when I run the django server and visit from browser it gives me errors:
http://localhost:8000/gallery/image/Hunt-Club/shop/1/” cannot be displayed because it contains errors.">
I do not know want I give correct path and read the data, there is still error for picture require.
You may want to rethink serving files through the django app.
Instead you should serve them from /static folder which in an apache setup you would configure using an alias.
That being said, check this out: image/pjpeg and image/jpeg
It says it may have something to do with serving pjpeg content-type to IE.
Hope that helps.
i find where my error is, the read image file code should be:
fd = open(CONFIG.SERVICES_PATH + sname+'/'+url,'rb')
Because in windows open file default applying open in ASCII, so the file doesn't read properly.

Categories

Resources