1 분 소요

?


failed to load resource the server responded with a status of 403

게시글에 올라가는 이미지들은 모두 google drive에 저장해서 링크만 가져와 사용하는 편인데 언체부터 다음 에러가 발생하면서
첨부했던 모든 이미지가 엑박이 뜨는 문제가 있었다. 그런데 링크만 따와서 브라우저에 입력하면 접속은 되는 요상한 상황이었다!

403 에러는 권한 문제다보니까 구글에서 일부러 외부 출처에서 사용하지 못하도록 막아놓은게 아닐까? 라는 뇌피셜을 살짝 했지만
이러나 저러나 해결할 순 없으니 일단 구글링을 해봤고, 도움되는 내용을 찾을 수 있었다!

나같은 경우 이미지 링크를 https://drive.google.com/uc?id={이미지 ID} 형태로 만들어서 활용하고 있었는데,
이걸 https://lh3.google.com/u/0/d/{이미지 ID}로 변경하니깐 문제가 다 해결됐다!

작성했던 모든 파일에 들어가서 수정해주긴 너무 오래 걸려서 디렉토리 내 모든 파일에 접근해 **/uc?id가 포함된 위 주소를
모두 찾아 아래 주소(**/u/0/d)로 수정해주는 python 코드를 실행했다.

import os
import re

# 현재 스크립트 파일의 경로
script_path = os.path.abspath(__file__)

# 현재 스크립트 파일이 위치한 디렉토리
current_directory = os.path.dirname(script_path)

# 대상 디렉토리 설정
directory_path = current_directory

# 정규표현식 패턴 설정
pattern = re.compile(r'<img src="https://drive.google.com/uc\?id=(.*?)"(.*?)\/>')

# 대체할 문자열 설정
replacement = '<img src="https://lh3.google.com/u/0/d/\\1"\\2/>'

# 대상 디렉토리 내의 모든 .md 파일에 대해 작업 수행
for filename in os.listdir(directory_path):
    if filename.endswith(".md"):
        file_path = os.path.join(directory_path, filename)
        with open(file_path, 'r') as file:
            lines = file.readlines()
        with open(file_path, 'w') as file:
            for line in lines:
                modified_line = re.sub(pattern, replacement, line)
                file.write(modified_line)

print("작업 완료")

Reference

카테고리:

업데이트:

댓글남기기