{"id":53234,"date":"2024-12-03T14:28:49","date_gmt":"2024-12-03T06:28:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53234\/"},"modified":"2024-12-03T14:28:49","modified_gmt":"2024-12-03T06:28:49","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-nodejsd-%e4%b8%ad%e9%9b%86%e6%88%90-cloudinary-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53234\/","title":{"rendered":"\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/17327682516747f1fbe60cd.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u63d2\u56fe\" \/><\/p>\n<p>\u9ad8\u6548\u7ba1\u7406\u56fe\u50cf\u548c\u89c6\u9891\u7b49\u5a92\u4f53\u8d44\u4ea7\u5bf9\u4e8e web \u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\uff0ccloudinary \u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fa\u8272\u7684\u89e3\u51b3\u65b9\u6848\u6765\u65e0\u7f1d\u5904\u7406\u8fd9\u4e9b\u8d44\u4ea7\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u4ecb\u7ecd cloudinary \u5728 node.js \u9879\u76ee\u4e2d\u7684\u96c6\u6210\u8fc7\u7a0b\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f\u4e91\uff1f <\/h2>\n<p>cloudinary \u662f\u4e00\u79cd\u57fa\u4e8e\u4e91\u7684\u5a92\u4f53\u7ba1\u7406\u670d\u52a1\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4ee5 web \u53cb\u597d\u7684\u683c\u5f0f\u8f7b\u677e\u5b58\u50a8\u3001\u8f6c\u6362\u548c\u4ea4\u4ed8\u56fe\u50cf\u548c\u89c6\u9891\u3002\u51ed\u501f\u81ea\u52a8\u56fe\u50cf\u4f18\u5316\u3001\u54cd\u5e94\u5f0f\u8f6c\u6362\u548c\u901a\u8fc7 cdn \u8fdb\u884c\u5185\u5bb9\u4ea4\u4ed8\u7b49\u529f\u80fd\uff0ccloudinary \u5df2\u6210\u4e3a\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u7684\u9996\u9009\u3002<br \/> \u63a2\u7d22 cloudinary \u5b9a\u4ef7<\/p>\n<h2> \u5148\u51b3\u6761\u4ef6 <\/h2>\n<p>\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\uff1a<\/p>\n<ul>\n<li>\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86 node.js<\/li>\n<li>\u57fa\u672c\u7684 node.js \u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e<\/li>\n<li>\u4e00\u4e2acloudinary\u5e10\u6237\uff08\u5982\u679c\u60a8\u6ca1\u6709\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u6ce8\u518c\uff09<\/li>\n<\/ul>\n<h2> \u7b2c1\u6b65\uff1a\u5b89\u88c5cloudinary sdk <\/h2>\n<pre>npm install cloudinary\n\n<\/pre>\n<h2> \u7b2c2\u6b65\uff1a\u914d\u7f6ecloudinary <\/h2>\n<pre>const cloudinary = require('cloudinary').v2;\n\ncloudinary.config({\n  cloud_name: 'your_cloud_name',\n  api_key: 'your_api_key',\n  api_secret: 'your_api_secret',\n});\n\nmodule.exports = cloudinary;\n\n<\/pre>\n<h2> \u7b2c\u4e09\u6b65\uff1a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf <\/h2>\n<pre>cloud_name=your-cloud-name\napi_key=your-api-key\napi_secret=your-api-secret\n\n<\/pre>\n<pre>npm install dotenv\n<\/pre>\n<pre>require('dotenv').config();\n\ncloudinary.config({\n  cloud_name: process.env.cloud_name,\n  api_key: process.env.api_key,\n  api_secret: process.env.api_secret,\n});\n\n<\/pre>\n<h2> \u7b2c4\u6b65\uff1a\u5c06\u56fe\u50cf\u4e0a\u4f20\u5230cloudinary <\/h2>\n<pre>const cloudinary = require('.\/config');\n\nasync function uploadimage(imagepath) {\n  try {\n    const result = await cloudinary.uploader.upload(imagepath, {\n      folder: 'samples', \/\/ optional: specify the folder to store images\n    });\n    console.log('image uploaded successfully:', result.url);\n    return result.url;\n  } catch (error) {\n    console.error('error uploading image:', error);\n  }\n}\n\n\/\/ example usage\nuploadimage('path\/to\/your\/image.jpg');\n\n<\/pre>\n<h2> \u7b2c 5 \u6b65\uff1a\u4f7f\u7528 cloudinary \u8f6c\u6362\u56fe\u50cf <\/h2>\n<pre>const transformedimageurl = cloudinary.url('sample.jpg', {\n  width: 400,\n  height: 300,\n  crop: 'fill',\n});\n\nconsole.log('transformed image url:', transformedimageurl);\n\n<\/pre>\n<h2> \u7b2c 6 \u6b65\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406\u6587\u4ef6\u4e0a\u4f20 <\/h2>\n<pre>npm install multer\n\n<\/pre>\n<pre>const multer = require('multer');\nconst upload = multer({ dest: 'uploads\/' }); \/\/ temporary folder for uploaded files\n\napp.post('\/upload', upload.single('image'), async (req, res) =&gt; {\n  try {\n    const imagepath = req.file.path;\n    const imageurl = await uploadimage(imagepath);\n    res.json({ imageurl });\n  } catch (error) {\n    res.status(500).json({ error: 'failed to upload image' });\n  }\n});\n\n<\/pre>\n<h2> \u7b2c 7 \u6b65\uff1a\u4f18\u5316\u548c\u4ea4\u4ed8\u5a92\u4f53\u8d44\u4ea7 <\/h2>\n<pre>const optimizedImageUrl = cloudinary.url('sample.jpg', {\n  fetch_format: 'auto',\n  quality: 'auto',\n});\n\nconsole.log('Optimized Image URL:', optimizedImageUrl);\n\n<\/pre>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5c06 cloudinary \u96c6\u6210\u5230\u60a8\u7684 node.js \u9879\u76ee\u4e2d\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u6253\u5f00\u4e86\u5a92\u4f53\u7ba1\u7406\u529f\u80fd\u7684\u4e16\u754c\u3002\u65e0\u8bba\u60a8\u5904\u7406\u56fe\u50cf\u8fd8\u662f\u89c6\u9891\uff0ccloudinary \u5f3a\u5927\u7684 api \u90fd\u53ef\u4ee5\u8ba9\u60a8\u8f7b\u677e\u9ad8\u6548\u5730\u4f18\u5316\u3001\u8f6c\u6362\u548c\u4ea4\u4ed8\u8d44\u4ea7\u3002<\/p>\n<p>\u7f16\u7801\u6109\u5feb\uff01<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528\u539f\u751f JavaScript \u64cd\u4f5c DOM \u5e76\u63d2\u5165\u6216\u5220\u9664 HTML \u5185\u5bb9\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary \u6536\u85cf \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u9ad8\u6548\u7ba1\u7406\u56fe\u50cf\u548c\u89c6\u9891\u7b49\u5a92\u4f53\u8d44\u4ea7\u5bf9\u4e8e web \u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\uff0ccloudinary \u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fa\u8272\u7684\u89e3\u51b3\u65b9\u6848\u6765\u65e0\u7f1d\u5904\u7406\u8fd9\u4e9b\u8d44\u4ea7\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u4ecb\u7ecd cloudinary \u5728 node.js \u9879\u76ee\u4e2d\u7684\u96c6\u6210\u8fc7\u7a0b\u3002 \u4ec0\u4e48\u662f\u4e91\uff1f cloudinary \u662f\u4e00\u79cd\u57fa\u4e8e\u4e91\u7684\u5a92\u4f53\u7ba1\u7406\u670d\u52a1\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4ee5 web \u53cb\u597d\u7684\u683c\u5f0f\u8f7b\u677e\u5b58\u50a8\u3001\u8f6c\u6362\u548c\u4ea4\u4ed8\u56fe\u50cf\u548c\u89c6\u9891\u3002\u51ed\u501f\u81ea\u52a8\u56fe\u50cf\u4f18\u5316\u3001\u54cd\u5e94\u5f0f\u8f6c\u6362\u548c\u901a\u8fc7 cdn \u8fdb\u884c\u5185\u5bb9\u4ea4\u4ed8\u7b49\u529f\u80fd\uff0ccloudinary \u5df2\u6210\u4e3a\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u7684\u9996\u9009\u3002 \u63a2\u7d22 cloudinary \u5b9a\u4ef7 \u5148\u51b3\u6761\u4ef6 \u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\uff1a \u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86 node.js \u57fa\u672c\u7684 node.js \u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e \u4e00\u4e2acloudinary\u5e10\u6237\uff08\u5982\u679c\u60a8\u6ca1\u6709\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u6ce8\u518c\uff09 \u7b2c1\u6b65\uff1a\u5b89\u88c5cloudinary sdk npm install cloudinary \u7b2c2\u6b65\uff1a\u914d\u7f6ecloudinary const cloudinary = require(&#8216;cloudinary&#8217;).v2; cloudinary.config({ cloud_name: &#8216;your_cloud_name&#8217;, api_key: &#8216;your_api_key&#8217;, api_secret: &#8216;your_api_secret&#8217;, }); module.exports [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53234","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53234"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53234\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}