Office365-rest-python-client: get_file_by_server_relative_url์„ ํ†ตํ•ด ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2020๋…„ 05์›” 21์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: vgrem/Office365-REST-Python-Client

@vgrem ๋‹˜ , ๋‹ค์Œ ์ฝ”๋“œ๋Š” ValueError ์‹œํ‚ต๋‹ˆ๋‹ค.

...
web = client_context.web
client_context.load(web)
server_relative_url = f'/{folder_name}/{filename}'
logger.info(f'relative path: {server_relative_url}')
file = web.get_file_by_server_relative_url(server_relative_url)
return file.read()

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด๋‹น ์˜ค๋ฅ˜์— ์ผ๋ถ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
```
raise ValueError('๋‹น์‹ ์€ ์ด๊ฒƒ๊ณผ ์ €๊ฒƒ์„ ์ž˜๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค...')

question

๋ชจ๋“  4 ๋Œ“๊ธ€

file.read() ์ „์— ์ด๊ฒƒ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋‚ด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค.

client_context.load(file)
client_context.execute_query()

๋งž์Šต๋‹ˆ๋‹ค. File.read ๋ฉ”์†Œ๋“œ๋Š” ํŒŒ์ผ์ด ๋‹ค์šด๋กœ๋“œ๋˜๊ธฐ ์ „์— ํŒŒ์ผ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ(ํŠนํžˆ ํŒŒ์ผ ์ฃผ์†Œ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ServerRelativeUrl ์†์„ฑ)๊ฐ€ ๊ฒ€์ƒ‰๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค.

client_context.load(file)  
client_context.execute_query()

ํŒŒ์ผ ์ฝ˜ํ…์ธ ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ง๊ด€์ ์ด์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ File.download ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

with open(download_path, "wb") as local_file:
    source_file = ctx.web.get_file_by_server_relative_url(file_url)
    source_file.download(local_file)
    ctx.execute_query()

@vgrem ์—๊ฒŒ ๊ฐ์‚ฌ

file.download๋Š” 2.1.7.post1์˜ ์ผ๋ถ€์ž…๋‹ˆ๊นŒ?

pip์˜ ์ตœ์‹  ๋ฒ„์ „์€ ๊ทธ ์ •๋„๊นŒ์ง€๋งŒ ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ• ๋ชฉ๋ก | grep -i ์‚ฌ๋ฌด์‹ค
Office365-REST-ํŒŒ์ด์ฌ-ํด๋ผ์ด์–ธํŠธ 2.1.7.post1

2.1.8
pip ์„ค์น˜ --์—…๊ทธ๋ ˆ์ด๋“œ Office365-REST-Python-Client==2.1.8
์˜ค๋ฅ˜: Office365-REST-Python-Client==2.1.8 ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•˜๋Š” ๋ฒ„์ „์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค(๋ฒ„์ „: 1.0.0, 1.0.1, 1.1.0, 2.0.0, 2.1.1, 2.1.2). , 2.1.3, 2.1.4, 2.1.5, 2.1.6.post2, 2.1.7.post1)
์˜ค๋ฅ˜: Office365-REST-Python-Client==2.1.8์— ๋Œ€ํ•ด ์ผ์น˜ํ•˜๋Š” ๋ฐฐํฌํŒ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

==== ์ž„์‹œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ====

์ž„์‹œ ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ
์ˆ˜์ž… OS
๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ = os.path.join(tempfile.mkdtemp(), os.path.basename(์œ„์น˜))
file_url = '/์‚ฌ์ดํŠธ/ํ†ตํ•ฉ/๊ณต์œ  ๋ฌธ์„œ/SomeFolder/somefile.csv'
๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ
open(download_path, "wb")์„ local_file๋กœ ์‚ฌ์šฉ:
์†Œ์Šค ํŒŒ์ผ = ctx.web.get_file_by_server_relative_url(file_url)
์†Œ์Šค ํŒŒ์ผ
ctx.load(์†Œ์Šค ํŒŒ์ผ)
ctx.execute_query()
r = source_file.read()
ctx.execute_query()
print("๋ฐ์ดํ„ฐ:", r)
local_file.write(r)
local_file.close()
print("[ํ™•์ธ] ํŒŒ์ผ์ด ๋‹ค์šด๋กœ๋“œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: {0}".format(download_path))

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰