2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

容量無制限Dropboxで容量無制限Imgurを作る

Last updated at Posted at 2022-04-16

See the Pen Dropbox by John Doe (@04) on CodePen.

Dropboxが容量無制限になったので実質容量無制限の画像共有サイト作ってみました
ファイルをアップロードして、共有リンクを作成できます。

async function uploadFile(file) {
  const now = new Date()
    .toLocaleString("sv")
    .replace(" ", "_")
    .replaceAll(":", "_");
  const path = "/" + now + "-" + file.name.replace(/[^\x00-\x7F]/g, "");
  await fetch("https://content.dropboxapi.com/2/files/upload", {
    body: file,
    method: "POST",
    headers: {
      "Content-Type": "application/octet-stream",
      "Dropbox-API-Arg": JSON.stringify({ path }),
      Authorization: "Bearer " + TOEKN
    }
  });
  const res = await fetch(
    "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings",
    {
      body: JSON.stringify({
        path,
        settings: {
          access: "viewer",
          allow_download: true,
          audience: "public"
        }
      }),
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer " + TOEKN
      }
    }
  ).then((r) => r.json());
  return res.url
    .replace("www.dropbox.com", "dl.dropboxusercontent.com")
    .replace(/\?dl=0$/, "");
}

アクセストークンの作り方

リンクを開きます

https://www.dropbox.com/login?cont=https%3A%2F%2Fwww.dropbox.com%2Fdevelopers%2Fapps%3F_tk%3Dpilot_lp%26_ad%3Dtopbar4%26_camp%3Dmyapps

アプリを作成し、Permissionsにチェックをいれます

image.png

Settingsでアクセストークンを生成する

image.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?