我可以在SharePoint中将文件上传到Documents
。但我想把它上传到一个特定的文件夹。我不知道我该怎么做。欢迎任何帮助。我正在使用下面的代码上传文件。
def upload_file(ctx, listTitle, path):
list_obj = ctx.web.lists.get_by_title(listTitle)
folder = list_obj.root_folder
ctx.load(folder)
ctx.execute_query()
files = folder.files
ctx.load(files)
ctx.execute_query()
with open(path, 'rb') as f:
content = f.read()
file_creation_information = FileCreationInformation()
file_creation_information.overwrite = True
file_creation_information.url = os.path.basename(path)
file_creation_information.content = content
file_new = files.add(file_creation_information)
ctx.load(files)
ctx.execute_query()
upload_file(ctx,'/Documents/reports/',path)
上面的代码适用于upload_file(ctx,'Documents',report)
但不适用于CCD_ 3文件夹。它不起作用。
错误:
office365.runtime.client_request_exception.ClientRequestException: ('-1, System.ArgumentException', "List 'reports' does not exist at site with URL 'https://sharepoint.com/sites/my_page'.", "404 Client Error: Not Found for url: https://sharepoint.com/sites/my_page/_api/Web/lists/GetByTitle('reports')/RootFolder")
您得到的错误建议您以某种方式查找名为reports
的列表。但是您想要得到一个文件夹对象。
目前还不清楚你用哪个包连接到sharepoint,但文档说你需要进行这样的api调用:
POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)
Authorization: "Bearer " + accessToken
Content-Length: {length of request body as integer}
X-RequestDigest: "{form_digest_value}"
"Contents of file"
将文件上载到Document目录是可行的,因为它是默认目录Shared Documents中的一个文件夹。您只能通过.root_folder
调用访问根目录中的文件夹。
编辑2:由于我不知道你使用的是哪个包(可能是Office365REST(,你可以在正常的requests.post
函数中使用上面的端点上传文件。
应该是这样的:
import requests
url = f'https://{site_url}/_api/web/GetFolderByServerRelativeUrl('{specific Folder URL}')/Files/add(url='a.txt',overwrite=true)'
headers = {Authorization: "Bearer " + accessToken,
Content-Length: content-length
}
payload = your_byte_string
response = requests.post(url = url, headers=headers, data = payload}