Splunk Dev

Python SDK file upload issue

HafizEAhmed
Explorer

I'm testing uploading a file using the Splunk Python SDK and receiving the following error :
Traceback (most recent call last):
File "testConnection.py", line 50, in
myindex.upload("/opt/splunkforwarder/bin/scripts/test");
File "/home/hafahmed/splunk-sdk-python-1.6.6/splunklib/client.py", line 2106, in upload
self.service.post(path, name=filename, **kwargs)
File "/home/hafahmed/splunk-sdk-python-1.6.6/splunklib/binding.py", line 289, in wrapper
return request_fun(self, *args, **kwargs)
File "/home/hafahmed/splunk-sdk-python-1.6.6/splunklib/binding.py", line 71, in new_f
val = f(*args, **kwargs)
File "/home/hafahmed/splunk-sdk-python-1.6.6/splunklib/binding.py", line 752, in post
response = self.http.post(path, all_headers, **query)
File "/home/hafahmed/splunk-sdk-python-1.6.6/splunklib/binding.py", line 1224, in post
return self.request(url, message)
File "/home/hafahmed/splunk-sdk-python-1.6.6/splunklib/binding.py", line 1244, in request
raise HTTPError(response)
splunklib.binding.HTTPError: HTTP 400 Bad Request -- unable to open file: path='/opt/splunkforwarder/bin/scripts/test' error='No such file or directory'

Code snippet:
myindex = service.indexes["testing"]

myindex.upload("/opt/splunkforwarder/bin/scripts/test");

The file exists and has the correct permissions, Any hints?

Labels (2)
Tags (2)

rsokolova
Path Finder

I think the problem might be where the file is located, if the file is in another server it won't work. In other words, you first need to copy the file to Splunk SH server then you can upload it. Once the file is locally you can pass the path and call the function to upload the file.

SCP the file to /tmp folder then:

myindex = service.indexes["secdevops"]

uploadme = "/tmp/scoring_output.csv"

myindex.upload(uploadme);

0 Karma

burakatabay
Path Finder

unable to open file it's like permission problem ?

chmod a+x yourscript.py Or yourscrpit.sh

can you try ?

0 Karma

codebuilder
SplunkTrust
SplunkTrust

Your file does not have an extension. Splunk interprets files with no extension as binary files and will not ingest them.

----
An upvote would be appreciated and Accept Solution if it helps!
0 Karma

HafizEAhmed
Explorer

hmm tried changing the extension but I'm still getting the same error

splunklib.binding.HTTPError: HTTP 400 Bad Request -- unable to open file: path='/opt/splunkforwarder/bin/scripts/test.txt' error='No such file or directory'

0 Karma

tom_frotscher
Builder

Can you post the actual lines of python code for the upload?

0 Karma

HafizEAhmed
Explorer

myindex = service.indexes["testing"]
uploadme = "/opt/splunkforwarder/bin/scripts/test.txt"
myindex.upload(uploadme)

0 Karma

tom_frotscher
Builder

This method does not use a path, it just uses a file name as parameter. Try to change the directory,and then try to use just "text.txt" as the parameter for the upload function. You can change the directory in python with os.chdir see example here: https://stackoverflow.com/questions/431684/how-do-i-change-directory-cd-in-python

Currently, you tried to upload a file that is called "opt/Splunk...." in your current working directory, that's why the file is not found.

0 Karma

codebuilder
SplunkTrust
SplunkTrust

Is the file and directory owned by splunk?

----
An upvote would be appreciated and Accept Solution if it helps!
0 Karma

tom_frotscher
Builder

Hi,

is there no file extention like test.sh or test.py? Looks like your path is pointing to a folder, not a file.
Which permissions does the file have? Is it readable and executable?

0 Karma

HafizEAhmed
Explorer

it's actually pointing to a file and it's both readable and executable 😞

0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...