我正在尝试使用Nifi的ExecuteScript处理器将PDF文件上传到Confluence。我可以成功上传文件,但是当我下载并打开它时,它是空白。我的转换一定有问题。有人可以帮忙检查吗?
这就是我的方法:
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
flowFile = session.get()
if(!flowFile)return
def text = ''
session.read(flowFile, {inputStream ->
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
} as InputStreamCallback)
flowFile = session?.putAttribute(flowFile, "file_content", text)
session.transfer(flowFile, /*ExecuteScript.*/ REL_SUCCESS)
3. ExecuteScript Python-将PDF文件上传到Confluence
这是我的#3代码。我认为这里有问题->
import json
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
from org.apache.nifi.processor.io import OutputStreamCallback
class OutputWrite(OutputStreamCallback):
def __init__(self, obj):
self.obj = obj
def process(self, outputStream):
outputStream.write(bytearray(json.dumps(self.obj).encode('utf-8')))
flowFile = session.get()
if (flowFile != None):
url = 'https://myconfluence.com/rest/api/content/12345/child/attachment'
auth = 'myauthorization'
file_name = 'mypdf.pdf'
file_content = flowFile.getAttribute('file_content')
s = requests.Session()
m = MultipartEncoder(fields={'file': (file_name, file_content, 'application/pdf')})
headers = {"X-Atlassian-Token":"nocheck", "Authorization":auth, "Content-Type":m.content_type}
r = s.post(url, data=m, headers=headers, verify=False)
session.write(flowFile, OutputWrite(json.loads(r.text)))
session.transfer(flowFile, REL_SUCCESS)
session.commit()
更新06/28/2019
我决定听从Peter的建议,合并代码1和2。它仍然无法正常工作。以前,PDF文件为2MB,但为空白。现在,它的大小为0KB。任何帮助将不胜感激!
import json
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
from org.apache.nifi.processor.io import OutputStreamCallback
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import InputStreamCallback
class PyInputStreamCallback(InputStreamCallback):
def __init__(self):
pass
def process(self, inputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
class OutputWrite(OutputStreamCallback):
def __init__(self, obj):
self.obj = obj
def process(self, outputStream):
outputStream.write(bytearray(json.dumps(self.obj).encode('utf-8')))
text = ''
flowFile = session.get()
if(flowFile != None):
session.read(flowFile, PyInputStreamCallback())
confluence_attachment_api = flowFile.getAttribute('confluence_attachment_api')
confluence_authorization = flowFile.getAttribute('confluence_authorization')
file_name = flowFile.getAttribute('file_name')
s = requests.Session()
m = MultipartEncoder(fields={'file': (file_name, text, 'application/pdf')})
headers = {"X-Atlassian-Token":"nocheck", "Authorization":confluence_authorization, "Content-Type":m.content_type}
r = s.post(confluence_attachment_api, data=m, headers=headers, verify=False)
session.write(flowFile, OutputWrite(json.loads(r.text)))
session.transfer(flowFile, REL_SUCCESS)
session.commit()
答案 0 :(得分:1)
看起来您实际上不是在发送FlowFile内容。相反,您将发送一个名为file_content
的属性作为文件内容,这可能不是您想要的
您将需要执行session.read
来获取文件流。以下代码无法按原样工作,但显示了如何访问该流。
class PyInputStreamCallback(InputStreamCallback):
def __init__(self):
pass
def process(self, inputStream):
m = MultipartEncoder(fields={'file': (file_name, inputStream, 'application/pdf')})
session.read(flowFile, PyInputStreamCallback())
参考:https://community.hortonworks.com/articles/75545/executescript-cookbook-part-2.html